码迷,mamicode.com
首页 > 其他好文 > 详细

File FileInputStream和FileOutputStream读写

时间:2015-10-22 19:20:32      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.javaio.study;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 
  8 /**
  9  * FileInputStream和FileOutputStream学习
 10  * @author chen
 11  *
 12  */
 13 public class IOUtils {
 14 
 15     /**
 16      * 读取指定文件内容,按照16进制输出到控制台
 17      * 并且每输出10个byte换行
 18      * @throws IOException 
 19      * 单字节读取不适合大文件,大文件效率很低
 20      */
 21     private void printHex(String fileName) throws IOException{
 22         
 23         //把文件作为字节流进行读操作
 24         FileInputStream fis = new FileInputStream(fileName);
 25         int b;
 26         int i = 1;
 27         System.out.println("-----------单字节读取-----------");
 28         while((b = fis.read()) != -1){
 29             if(b <= 0xf){
 30                 //单位数前面补零
 31                 System.out.print("0");
 32             }
 33             System.out.print(Integer.toHexString(b) + " ");
 34             if(i++ % 10 == 0){
 35                 System.out.println();
 36             }
 37         }
 38         fis.close();
 39     }
 40     
 41     /**
 42      * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
 43      * @param fileName
 44      * @throws IOException
 45      */
 46     private void printHexByByteArray(String fileName) throws IOException{
 47         
 48         FileInputStream fis = new FileInputStream(fileName);
 49         byte[] buf = new byte[20*1024];
 50         /*
 51          * 从fis中批量读取字节,放入到buf这个字节数组中,从第0个位置开始放,
 52          * 最多放buf.length个,返回的是读到的字节的个数
 53          */
 54         System.out.println("\n\n-----------批量读取1------------");
 55         int bytes = fis.read(buf, 0, buf.length);//一次性读完,说明字节数组足够大
 56         int j = 1;
 57         for(int i = 0; i < bytes; i++){
 58             if((buf[i] & 0xff) <= 0xf){
 59                 System.out.print("0");
 60             }
 61             //byte类型8位,int类型32位,为了避免数据转换错误,通过&0xff将高24位清零
 62             System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
 63             if(j++ % 10 == 0){
 64                 System.out.println();
 65             }
 66         }
 67         fis.close();
 68         
 69         System.out.println("\n\n-----------批量读取2------------");
 70         FileInputStream fis2 = new FileInputStream(fileName);
 71         byte[] buf2 = new byte[20*1024];
 72         int bytes2 = 0;
 73         int j2 = 1;
 74         while((bytes2 = fis2.read(buf2, 0, buf2.length)) != -1){
 75             for(int i = 0; i < bytes2; i++){
 76                 if((buf2[i] & 0xff) <= 0xf){
 77                     System.out.print("0");
 78                 }
 79                 System.out.print(Integer.toHexString(buf2[i] & 0xff) + " ");
 80                 if(j2++ % 10 == 0){
 81                     System.out.println();
 82                 }
 83             }
 84         }
 85         
 86         fis2.close();
 87         
 88     }
 89     
 90     /**
 91      * FileOutputStream写入
 92      * @throws Exception
 93      */
 94     private void Write() throws Exception{
 95         
 96         //如果该文件不存在,则直接创建,如果存在,删除后创建
 97         FileOutputStream fos = new FileOutputStream("demo\\out.dat");
 98         fos.write(‘A‘);//写出了A的低八位
 99         fos.write(‘B‘);//写出了B的低八位
100         int a = 10;//write只能写八位,那么写一个int需要写4次,每次8位
101         fos.write(a >>> 24);
102         fos.write(a >>> 16);
103         fos.write(a >>> 8);
104         fos.write(a);
105         byte[] gbk = "中国".getBytes("UTF-8");
106         fos.write(gbk);
107         fos.close();
108         
109         System.out.println("\n\n-----------数据写入成功,读取如下-----------\n");
110         printHex("demo\\out.dat");
111         
112     }
113     
114     /**
115      * 文件复制
116      * @param srcFile
117      * @param destFile
118      * @throws IOException
119      */
120     private void copyFile(File srcFile, File destFile) throws IOException{
121         
122         if(!srcFile.exists()){
123             throw new IllegalArgumentException("文件:" + srcFile + "不存在!");
124         }else if(!srcFile.isFile()){
125             throw new IllegalArgumentException(srcFile + "不是文件!");
126         }
127         
128         FileInputStream fis = new FileInputStream(srcFile);
129         FileOutputStream fos = new FileOutputStream(destFile);
130         byte[] buf = new byte[8*1024];
131         int b;
132         while((b = fis.read(buf, 0, buf.length)) != -1){
133             fos.write(buf, 0, b);
134             fos.flush();//最好加上
135         }
136         System.out.println("\n\n-----------复制成功-----------");
137         
138         fos.close();
139         fis.close();
140         
141     }
142     
143     public static void main(String[] args) throws Exception {
144         
145         IOUtils iou = new IOUtils();
146         
147 //        long start = System.currentTimeMillis();
148         
149         iou.printHex("E:\\Project-Java\\JavaIO\\src\\com\\javaio\\study\\IOUtils.java");
150         
151         iou.printHexByByteArray("E:\\Project-Java\\JavaIO\\src\\com\\javaio\\study\\IOUtils.java");
152         
153 //        long end = System.currentTimeMillis();
154 //        System.out.println("\n==============================");
155 //        System.out.println("花费时间(ms):" + (end - start));
156         
157         iou.Write();
158         
159         iou.copyFile(new File("E:\\Project-Java\\JavaIO\\src\\com\\javaio\\study\\IOUtils.java"), new File("demo\\out.dat"));
160         
161     }
162     
163 }

 

参考:慕课网(http://www.imooc.com/)

File FileInputStream和FileOutputStream读写

标签:

原文地址:http://www.cnblogs.com/jinjiyese/p/4902327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!