IO

IO

流是一个抽象、动态的概念,是一连串连续动态的数据集合。
Java中流的概念细分
1. 输入流:数据流向是数据源到程序(以InputStream、Reader结尾的流)。
2. 输出流:数据流向是程序到目的地(以OutPutStream、Writer结尾的流)。

按处理的数据单元分类:
1. 字节流:以字节为单位获取数据,命名上以Stream结尾的流一般是字节流,如FileInputStream、FileOutputStream。
2. 字符流:以字符为单位获取数据,命名上以Reader/Writer结尾的流一般是字符流,如FileReader、FileWriter。


Java中IO流类的体系


很多流都是成对出现的,比如:FileInputStream/FileOutputStream

1. InputStream/OutputStream:字节流的抽象类。

2. Reader/Writer:字符流的抽象类。

3. FileInputStream/FileOutputStream:节点流:以字节为单位直接操作“文件”。

4. ByteArrayInputStream/ByteArrayOutputStream:节点流:以字节为单位直接操作“字节数组对象”。

5. ObjectInputStream/ObjectOutputStream: 处理流:以字节为单位直接操作“对象”。

6. DataInputStream/DataOutputStream: 处理流:以字节为单位直接操作“基本数据类型与字符串类型”。

7. FileReader/FileWriter:节点流:以字符为单位直接操作“文本文件”(注意:只能读写文本文件)。

8. BufferedReader/BufferedWriter:处理流:将Reader/Writer对象进行包装,增加缓存功能,提高读写效率。

9. BufferedInputStream/BufferedOutputStream:处理流:将InputStream/OutputStream对象进行包装,增加缓存功能,提高 读写效率。

10. InputStreamReader/OutputStreamWriter: 处理流:将字节流对象转化成字符流对象。

11. PrintStream:处理流:将OutputStream进行包装,可以方便地输出字符,更加灵活。
四大IO抽象类
InputStream/OutputStream和Reader/writer类是所有IO流类的抽象父类

InputStream
int read():读取一个字节的数据,并将字节的值作为int类型返回(0-255之间的一个值)。如果未读出字节则返回-1(返回值为-1表示读取结束)。
void close():关闭输入流对象,释放相关系统资源。
OutputStream
void write(int n):向目的地中写入一个字节。
void close():关闭输出流对象,释放相关系统资源。
Reader
int read(): 读取一个字符的数据,并将字符的值作为int类型返回(0-65535之间的一个值,即Unicode值)。如果未读出字符则返回-1(返回值为-1表示读取结束)。
void close() : 关闭流对象,释放相关系统资源。
Reader
Writer用于写入的字符流抽象类,数据单位为字符。
void write(int n): 向输出流中写入一个字符。
void close() : 关闭输出流对象,释放相关系统资源。

理解IO操作步骤
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放流

 1 package boom.io;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 /**
10  * 理解IO操作步骤
11  * 1、创建源
12  * 2、选择流
13  * 3、操作
14  * 4、释放流
15  * @author Administrator
16  *
17  */
18 public class IOTest1 {
19 
20     public static void main(String[] args) {
21         // 1、选择流
22         File src = new File("abc.txt");
23         // 2、选择流
24         try {
25             InputStream is = new FileInputStream(src);
26             // 3、操作(读取)
27             int data1 = is.read();// 第一个数据
28             int data2 = is.read();// 第二个数据
29             int data3 = is.read();// 第三个数据
30             int data4 = is.read();// 第四个数据
31             // 返回字节对应的ASCII吗
32             System.out.println(data1);// 97
33             System.out.println(data2);// 98
34             System.out.println(data3);// 99
35             System.out.println(data4);// 没有这个数据存在就返回-1
36             
37             System.out.println();
38             
39             // 强转成字符
40             System.out.println((char)data1);// a
41             System.out.println((char)data2);// b
42             System.out.println((char)data3);// c
43             System.out.println((char)data4);
44             // 4、释放
45             is.close();
46             
47         } catch (FileNotFoundException e) {
48             e.printStackTrace();
49         } catch (IOException e) {
50             e.printStackTrace();
51         }
52     }
53 
54 }
View Code

IO标准的步骤

 1 package boom.io;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 /**
10  * 标准的IO流操作步骤
11  * 1、创建源
12  * 2、选择流
13  * 3、操作
14  * 4、释放流
15  * @author Administrator
16  *
17  */
18 public class IOTest2 {
19 
20     public static void main(String[] args) {
21         // 1、选择流
22         File src = new File("abc_1.txt");
23         // 2、选择流
24         InputStream is = null;
25         try {
26             is = new FileInputStream(src);
27             // 3、操作(读取)
28             int temp;
29             while ((temp = is.read()) != -1) {
30                 System.out.print((char) temp);
31             }
32 
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         } finally {
38             // 4、释放
39             try {
40                 if (is != null) {
41                     is.close();
42                 }
43             } catch (IOException e) {
44                 e.printStackTrace();
45             }
46         }
47     }
48 
49 }
View Code

FileInputStream:文件字节输入节点流。通过字节的方式读取文件,适合读取所有类型的文件(图像、视频等),全字符一般用FileReader.

 1 package boom.io;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 /**
10  * 文件字节输入流:FileInputStream,【分段读取】 1、创建源 2、选择流 3、操作 4、释放流
11  * 
12  * @author Administrator
13  *
14  */
15 public class IOTest3 {
16 
17     public static void main(String[] args) {
18     /*    // 1、创建源
19         File src = new File("abc.txt");
20         // 2、选择流
21         InputStream fis = null;*/
22         
23         FileInputStream fis = null;
24         try {
25             fis = new FileInputStream("abc.txt");
26             
27             fis.skip(3);// 跳过n个字节开始读取
28             // 3、操作流【分段读取】
29             byte[] flush = new byte[1024*10];// 缓冲容器
30             int len = -1;// 接收长度
31             while ((len = fis.read(flush)) != -1) {
32                 // 字节数组-->字符串(解码)
33                 String str = new String(flush, 0, len);
34                 System.out.println(str);
35             }
36 
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         } finally {
42             try {
43                 if (fis != null)
44                     fis.close();
45             } catch (IOException e) {
46                 e.printStackTrace();
47             }
48         }
49     }
50 
51 }
FileInputStream Code

FileInputStream:文件字节输出节点流。通过字节的方式写出或者追加数据到文件,适合读取所有类型的文件(图像、视频等),全字符一般用FileWrite.

 1 package boom.io;
 2 
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.OutputStream;
 8 
 9 /**
10  * 文件字节输出流:FileOutputStream,
11  * 1、创建源 2、选择流 3、操作(写出内容) 4、释放流
12  * @author Administrator
13  *
14  */
15 public class IOTest4 {
16 
17     public static void main(String[] args) {
18         /*// 1、创建源
19         File dest = new File("dest.txt");
20         // 2、选择流
21         OutputStream os  = null;*/
22         
23         FileOutputStream fos = null;
24         try {
25             fos = new FileOutputStream("dest.txt",true);// 重复执行会在默认追加相同的内容。默认的为false
26             // 操作(写出)
27             String msg = "桃花坞里桃花庵,桃花庵下桃花仙;桃花仙人种桃树,又摘桃花卖酒钱。";// 需要写出的内容
28             // 字符串-->字节数组(编码)
29             byte[] datas = msg.getBytes();
30             fos.write(datas, 0,datas.length);
31             fos.flush();// 刷新
32         } catch (FileNotFoundException e) {
33             e.printStackTrace();
34         } catch (IOException e) {
35             e.printStackTrace();
36         }finally{
37                 try {
38                     if(fos != null)
39                     fos.close();
40                 } catch (IOException e) {
41                     e.printStackTrace();
42                 }
43         }
44     }
45 
46 }
FileOutputStream Code

文件的拷贝

 1 package boom.io;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 /**
 9  * 文件的拷贝:文件字节输入、输出流
10  * 
11  * @author Administrator
12  *
13  */
14 public class CopyFile {
15 
16     public static void main(String[] args) {
17         copyfile("src\boom\io\CopyFile.java","copy.txt");
18 
19     }
20 
21     private static void copyfile(String srcPath,String descPath) {
22         // 1、选择两个流。先输出,在输出
23         FileInputStream fis = null;
24         FileOutputStream fos = null;
25         try {
26             // 2、具体操作 p.png 拷贝成 pcopy.png
27             fis = new FileInputStream(srcPath);// 源头
28             fos = new FileOutputStream(descPath);// 目的地
29             // 3、分段读取内容
30             byte[] flush = new byte[1024 * 10];
31             int len = -1;
32             while ((len = fis.read(flush)) != -1) {
33                 fos.write(flush, 0, len);// 分段写出
34             }
35             fos.flush();
36 
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         } finally {
42             // 4、分别关闭:先打开的后关闭
43             try {
44                 if (fos != null)
45                     fos.close();
46             } catch (IOException e) {
47                 e.printStackTrace();
48             }
49 
50             try {
51                 if (fis != null)
52                     fis.close();
53             } catch (IOException e) {
54                 e.printStackTrace();
55             }
56 
57         }
58 
59     }
60 
61 }
View Code

FileReader or FileWriter:文件字符输入、输出流

 1 package boom.io;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.FilterReader;
 7 import java.io.IOException;
 8 import java.io.Writer;
 9 
10 /**
11  * 文件字符输入、输出流
12  * 
13  * @author Administrator
14  *
15  */
16 public class IOTest5 {
17 
18     public static void main(String[] args) {
19         //TestFileReader();
20         TestFileWrite();
21     }
22 
23     private static void TestFileWrite() {
24         FileWriter fw = null;
25         try {
26             fw = new FileWriter("abc.txt",true);// true不打开默认是false,删除原来内容重新写信的,true是追加
27             // 写法一
28             /**
29             String msg = "桃花坞里桃花庵,桃花庵下桃花仙;桃花仙人种桃树,又摘桃花卖酒钱。";
30             char[] datas = msg.toCharArray();
31             fw.write(datas, 0, datas.length);
32             */
33             // 写法二
34             /**
35             String msg = "写法二,直接把要写出的字符扔进来";
36             fw.write(msg);
37             */
38             
39             // 写法三
40             fw.append("
开心!").append("就是笑");
41             
42             
43             fw.flush();
44         } catch (IOException e) {
45             e.printStackTrace();
46         }finally{
47             try {
48                 if(fw != null)
49                 fw.close();
50             } catch (IOException e) {
51                 e.printStackTrace();
52             }
53         }
54         
55     }
56 
57     private static void TestFileReader() {
58         // 选择流:文件字符输入流
59         FileReader fr = null;
60         try {
61             fr = new FileReader("abc.txt");
62             int len = -1;
63             // 字符缓冲器
64             char[] flush = new char[1024 * 10];
65             while ((len = fr.read(flush)) != -1) {
66                 String str = new String(flush, 0, len);
67                 System.out.println(str);
68             }
69         } catch (FileNotFoundException e) {
70             e.printStackTrace();
71         } catch (IOException e) {
72             e.printStackTrace();
73         } finally {
74             try {
75                 if (fr != null)
76                     fr.close();
77             } catch (IOException e) {
78                 e.printStackTrace();
79             }
80         }
81     }
82 }
View Code

字节数组:ByteArrayInputStream / ByteArrayOutputStream:节点流:以字节为单位直接操作“字节数组对象”。字节数组不要太大,释放资源可选处理。

原文地址:https://www.cnblogs.com/cao-yin/p/9651926.html