JAVA IO学习总结

一、java文件模型

  在硬盘上的文件是以byte byte byte ...格式存储的,是数据的集合。

二、java.io.File类用于表示文件(目录)

  File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问。以下是用File类的进本用法。

 1 package com.imooc.io;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class FileDemo {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         // 了解构造函数的情况  查帮助
13         File file = new File("E:\\javaio\\imooc");
14         //System.out.println(file.exists());
15         if(!file.exists())
16             file.mkdir(); //file.mkdirs()
17         else
18             file.delete();
19         //是否是一个目录  如果是目录返回true,如果不是目录or目录不存在返回的是false
20         System.out.println(file.isDirectory());
21         //是否是一个文件
22         System.out.println(file.isFile());
23         
24         //File file2 = new File("e:\\javaio\\日记1.txt");
25         File file2 = new File("e:\\javaio","日记1.txt");
26         if(!file2.exists())
27             try {
28                 file2.createNewFile();
29             } catch (IOException e) {
30                 // TODO Auto-generated catch block
31                 e.printStackTrace();
32             }
33         else 
34             file2.delete();
35          //常用的File对象的API
36         System.out.println(file);//file.toString()的内容
37         System.out.println(file.getAbsolutePath());
38         System.out.println(file.getName());
39         System.out.println(file2.getName());
40         System.out.println(file.getParent());
41         System.out.println(file2.getParent());
42         System.out.println(file.getParentFile().getAbsolutePath());
43     }
44 
45 }

三、java字节流

  以字节为单位读取或写入文件

3.1 InputStream、OutputStream

InputStream抽象了应用程序以字节流的方式从文件中读取数据的内存中的方法。

OutputStream抽象了应用程序以字节流的方式从内存中将数据写入到文件中的方法。

 

1)InputStream抽象了应用程序读取数据的方式,此抽象类是表示字节输入流的所有类的超类。 

输入流基本方法:
int b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF
in.read(byte[] buf) ;将读取到的字节写入到buf字节数组中。
in.read(byte[] buf,int start,int size);将读取到的字节写入到buf字节数组中,start表示写入到buf的开始位置(一般从0开始),size表示最大能写入的字节数(一般为buf的长度)。


2)OutputStream抽象了应用程序写出数据的方式,此抽象类是表示字节输出流的所有类的超类。

输出流的基本方法:

out.write(int b) ;写出一个byte到流,b的低8位
out.write(byte[] buf);将buf字节数组都写入到流
out.write(byte[] buf,int start,int size);

3)FileInputStream--->具体实现了在文件上读取数据
4)FileOutputStream 实现了向文件中写出数据的方法

以下是实例代码:

  1 package com.imooc.io;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 
 10 public class IOUtil {
 11     /**
 12      * 读取指定文件内容,按照16进制输出到控制台
 13      * 并且每输出10个byte换行
 14      * @param fileName
 15      * 单字节读取不适合大文件,大文件效率很低
 16      */
 17     public static void printHex(String fileName)throws IOException{
 18         //把文件作为字节流进行读操作
 19         FileInputStream in = new FileInputStream(fileName);
 20         int b ;
 21         int i = 1;
 22         while((b = in.read())!=-1){
 23             if(b <= 0xf){
 24                 //单位数前面补0
 25                 System.out.print("0");
 26             }
 27             System.out.print(Integer.toHexString(b)+"  ");
 28             if(i++%10==0){
 29                 System.out.println();
 30             }
 31         }
 32         in.close();
 33     }
 34     /**
 35      * 批量读取,对大文件而言效率高,也是我们最常用的读文件的方式
 36      * @param fileName
 37      * @throws IOException
 38      */
 39     public static void printHexByByteArray(String fileName)throws IOException{
 40         FileInputStream in = new FileInputStream(fileName);
 41         byte[] buf = new byte[8 * 1024];
 42         /*从in中批量读取字节,放入到buf这个字节数组中,
 43          * 从第0个位置开始放,最多放buf.length个 
 44          * 返回的是读到的字节的个数
 45         */
 46         /*int bytes = in.read(buf,0,buf.length);//一次性读完,说明字节数组足够大
 47         int j = 1; 
 48         for(int i = 0; i < bytes;i++){
 49             System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
 50             if(j++%10==0){
 51                 System.out.println();
 52             }
 53         }*/
 54       int bytes = 0;
 55       int j = 1;
 56       while((bytes = in.read(buf,0,buf.length))!=-1){
 57           for(int i = 0 ; i < bytes;i++){
 58               System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
 59               if(j++%10==0){
 60                   System.out.println();
 61               }
 62           }
 63       }
 64       in.close();
 65     }
 66     /**
 67      * 文件拷贝,字节批量读取
 68      * @param srcFile
 69      * @param destFile
 70      * @throws IOException
 71      */
 72     public static void copyFile(File srcFile,File destFile)throws IOException{
 73         if(!srcFile.exists()){
 74             throw new IllegalArgumentException("文件:"+srcFile+"不存在");
 75         }
 76         if(!srcFile.isFile()){
 77             throw new IllegalArgumentException(srcFile+"不是文件");
 78         }
 79         FileInputStream in = new FileInputStream(srcFile);
 80         FileOutputStream out = new FileOutputStream(destFile);
 81         byte[] buf = new byte[8*1024];
 82         int b ;
 83         while((b = in.read(buf,0,buf.length))!=-1){
 84             out.write(buf,0,b);
 85             out.flush();//最好加上
 86         }
 87         in.close();
 88         out.close();
 89         
 90     }
 91     /**
 92      * 进行文件的拷贝,利用带缓冲的字节流
 93      * @param srcFile
 94      * @param destFile
 95      * @throws IOException
 96      */
 97     public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
 98         if(!srcFile.exists()){
 99             throw new IllegalArgumentException("文件:"+srcFile+"不存在");
100         }
101         if(!srcFile.isFile()){
102             throw new IllegalArgumentException(srcFile+"不是文件");
103         }
104         BufferedInputStream bis = new BufferedInputStream(
105                 new FileInputStream(srcFile));
106         BufferedOutputStream bos = new BufferedOutputStream(
107                 new FileOutputStream(destFile));
108         int c ;
109         while((c = bis.read())!=-1){
110             bos.write(c);
111             bos.flush();//刷新缓冲区
112         }
113         bis.close();
114         bos.close();
115     }
116     /**
117      * 单字节,不带缓冲进行文件拷贝
118      * @param srcFile
119      * @param destFile
120      * @throws IOException
121      */
122     public static void copyFileByByte(File srcFile,File destFile)throws IOException{
123         if(!srcFile.exists()){
124             throw new IllegalArgumentException("文件:"+srcFile+"不存在");
125         }
126         if(!srcFile.isFile()){
127             throw new IllegalArgumentException(srcFile+"不是文件");
128         }
129         FileInputStream in = new FileInputStream(srcFile);
130         FileOutputStream out = new FileOutputStream(destFile);
131         int c ;
132         while((c = in.read())!=-1){
133             out.write(c);
134             out.flush();
135         }
136         in.close();
137         out.close();
138     }
139     
140 }

四、java字符流

1.字符流以字符为单位读取或写入数据,字符流只能处理文本和文本文件。

2.文本和文本文件

java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
文件是byte byte byte ...的数据序列
文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果

字符的处理,一次处理一个字符
字符的底层任然是基本的字节序列
字符流的基本实现
InputStreamReader 完成byte流解析为char流,按照编码解析
OutputStreamWriter 提供char流到byte流,按照编码处理

 1 package com.imooc.io;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.io.OutputStreamWriter;
 8 
 9 public class IsrAndOswDemo {
10     public static void main(String[] args)throws IOException {
11         FileInputStream in = new FileInputStream("e:\\javaio\\imoocutf8.txt");
12         InputStreamReader isr = new InputStreamReader(in,"utf-8");//默认项目的编码,操作的时候,要写文件本身的编码格式
13     
14         FileOutputStream out = new FileOutputStream("e:\\javaio\\imoocutf81.txt");
15         OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
16         /*int c ;
17         while((c = isr.read())!=-1){
18             System.out.print((char)c);
19         }*/
20         char[] buffer = new char[8*1024];
21         int c;
22         /*批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffer.length个
23           返回的是读到的字符的个数
24         */
25         while(( c = isr.read(buffer,0,buffer.length))!=-1){
26             String s = new String(buffer,0,c);
27             System.out.print(s);
28             osw.write(buffer,0,c);
29             osw.flush();
30         }
31         isr.close();
32         osw.close();
33         
34     }
35 
36 }

五、序列化

序列化接口(Serializable)
对象必须实现序列化接口 ,才能进行序列化,否则将出现异常
这个接口,没有任何方法,只是一个标准

 1 package com.imooc.io;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.ObjectInputStream;
 5 
 6 public class ObjectSeriaDemo1 {
 7     public static void main(String[] args) throws Exception{
 8         String file = "demo/obj.dat";
 9         //1.对象的序列化
10         /*ObjectOutputStream oos = new ObjectOutputStream(
11                 new FileOutputStream(file));
12         Student stu = new Student("10001", "张三", 20);
13         oos.writeObject(stu);
14         oos.flush();
15         oos.close();*/
16         ObjectInputStream ois = new ObjectInputStream(
17                 new FileInputStream(file));
18         Student stu = (Student)ois.readObject();
19         System.out.println(stu);
20         ois.close();
21         
22     }
23     
24 
25 }

 

posted @ 2015-07-17 12:25  Phil Li  阅读(271)  评论(0编辑  收藏  举报