IO流——各种流整理
Posted fangtingfei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO流——各种流整理相关的知识,希望对你有一定的参考价值。
最简单的,不加任何修饰的字节输入流
1 /*
2 * 最简单的,不加任何修饰的字节输入流
3 */
4 import java.io.FileInputStream;
5
6 public class Demo_01 {
7 public static void main(String[] args) throws Exception {
8 FileInputStream file=new FileInputStream("D:\myjavacode\IO\src\abc.txt");
9 int temp =-1;
10 while((temp=file.read())!=-1) {
11 System.out.println((char)temp);
12 }
13 file.close();
14 }
15 }
用数组做缓冲容器
1 import java.io.FileInputStream;
2
3 /*
4 * 用数组做缓冲容器
5 */
6 public class Demo_02 {
7 public static void main(String[] args) throws Exception {
8 FileInputStream file=new FileInputStream("D:\myjavacode\IO\src\abc.txt");
9 byte[] flush=new byte[1024]; //1M大小的缓冲,一次读取1024字节,提高效率
10 int len =-1;
11 while((len=file.read(flush))!=-1) {
12 String data=new String(flush,0,len);
13 System.out.println(data);
14 }
15 file.close();
16 }
17 }
字节文件输出流:
1 import java.io.FileOutputStream;
2
3 /*
4 * 字节文件输出流
5 */
6 public class Demo_03 {
7
8 public static void main(String[] args) throws Exception {
9 FileOutputStream file=new FileOutputStream("D:\myjavacode\IO\src\abc.txt",true); //可以选加Boolean类型,True表示可以追加写
10 String data="
hello world!";
11 byte[] datas=data.getBytes();
12 file.write(datas);
13 file.flush(); //每写入一次后要清理缓存
14 file.close();
15 }
16
17 }
文本文件字符输入流
1 import java.io.FileNotFoundException;
2 import java.io.FileReader;
3
4 /*
5 *文本文件字符输入流
6 */
7 public class Demo_04 {
8 public static void main(String[] args) throws Exception {
9 FileReader reader=new FileReader("D:\myjavacode\IO\src\abc.txt");
10 char[] flush=new char[100]; //建立一个缓冲区
11 int len=-1;
12 while((len=reader.read(flush))!=-1) {
13 String str=new String(flush,0,len);
14 System.out.println(str);
15 }
16 reader.close();
17 }
18 }
文本文件字符输出流
1 import java.io.FileWriter;
2 import java.io.IOException;
3
4 /*
5 * 文本文件字符输出流
6 */
7 public class Demo_05 {
8
9 public static void main(String[] args) throws Exception {
10 // TODO Auto-generated method stub
11 FileWriter writer=new FileWriter("D:\myjavacode\IO\src\abc.txt",true); //可以带是否追加的Boolean类型
12 char[] datas="hello".toCharArray();
13 writer.write(datas);
14 writer.append("hello");
15 writer.flush();
16 writer.close();
17 }
18 }
字节数组输入流
1 import java.io.ByteArrayInputStream;
2 import java.io.IOException;
3
4 /*
5 * 字节数组输入流
6 */
7 public class Demo_06 {
8
9 public static void main(String[] args) throws Exception {
10 byte[] datas="hello world1".getBytes();
11 ByteArrayInputStream by=new ByteArrayInputStream(datas);
12 byte[] flush=new byte[100]; //建立一个缓冲区
13 int len=-1;
14 while((len=by.read(flush))!=-1) {
15 String data=new String(flush,0,len);
16 System.out.println(data);
17 }
18 //字节数组流,不需要关闭,空方法
19 }
20 }
字节数组输出流:
1 import java.io.ByteArrayOutputStream;
2 import java.io.IOException;
3
4 /*
5 * 字节数组输出流
6 */
7 public class Demo_07 {
8 public static void main(String[] args) throws Exception {
9 ByteArrayOutputStream out=new ByteArrayOutputStream(); //字节数组输出流不指定资源,可以传一个int类型做缓冲区大小
10 byte[] datas="hello".getBytes();
11 out.write(datas); //写入流
12 out.flush();
13
14 //从流中读出
15 String data=new String(out.toByteArray());
16 System.out.println(data);
17 out.close();
18 }
19 }
由文件转换为字节数组流,再由字节数组流转换为文件
1 import java.io.ByteArrayInputStream;
2 import java.io.ByteArrayOutputStream;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6
7 /*
8 * 由文件转换为字节数组流,再由字节数组流转换为文件
9 */
10 public class Demo_08 {
11
12 //由文件转换为字节数组
13 public static byte[]FiletoByteArray(String filepath) throws Exception{
14 FileInputStream in=new FileInputStream(filepath);
15 ByteArrayOutputStream out=new ByteArrayOutputStream();
16 int len=-1;
17 byte[] brush=new byte[1024];
18 while((len=in.read(brush))!=-1) {
19 out.write(brush, 0, len);
20 out.flush();
21 }
22 in.close();
23 return out.toByteArray(); //字节数组流可以不关闭,关闭close()没有任何效果,空方法
24 }
25 //由字节数组转换为文件
26 public static void ByteArrayToFile(byte[] datas,String filename) throws Exception {
27 /*这样简单写应该也可以吧
28 FileOutputStream out = new FileOutputStream(filename);
29 out.write(datas);
30 out.flush();
31 out.close();
32 */
33 FileOutputStream out = new FileOutputStream(filename);
34 ByteArrayInputStream in=new ByteArrayInputStream(datas);
35 byte[] datass=new byte[1024];
36 int len=-1;
37 while((len=in.read(datass))!=-1) {
38 out.write(datass, 0, len);
39 out.flush();
40 }
41 out.close();
42 }
43
44 public static void main(String[] args) throws Exception {
45 byte[] datas=FiletoByteArray("C:\Users\user\Desktop\copy盲打指示.jpg");
46 ByteArrayToFile(datas,"D:\myjavacode\IO\src\IO_image.jpg");
47 }
48
49 }
装饰缓冲流BufferedInputStream
* Buffered作为装饰流只需套在节点流外面即可,提高了流的效率
* 字符缓冲流比字符流提供了更加便利的操作方法,比如读取下一行,换行等操作,操作字符流最好都加Buffered缓冲流
* 下面演示了BufferedReader和BufferedWriter字符缓冲流类
1 import java.io.BufferedReader;
2 import java.io.BufferedWriter;
3 import java.io.FileReader;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 /*
7 * 装饰缓冲流BufferedInputStream
8 * Buffered作为装饰流只需套在节点流外面即可,提高了流的效率
9 * 字符缓冲流比字符流提供了更加便利的操作方法,比如读取下一行,换行等操作
10 * 下面演示了BufferedReader和BufferedWriter字符缓冲流类
11 *
12 */
13 public class Demo_09 {
14
15 public static void main(String[] args) throws IOException {
16 BufferedReader reader=new BufferedReader(new FileReader("D:\myjavacode\IO\src\abc.txt"));
17 BufferedWriter writer=new BufferedWriter(new FileWriter("D:\myjavacode\IO\src\abc_1.txt"));
18 String line;
19 while((line=reader.readLine())!=null) { //逐行读取
20 System.out.println(line);
21 writer.write(line);
22 writer.newLine();
23 writer.flush();
24 }
25 writer.close();
26 reader.close();
27 }
28
29 }
转换流,由字节流转换为字符流,字符流转换为字节流
* InputStreamReader和InputStreamWriter
1 import java.io.BufferedReader;
2 import java.io.BufferedWriter;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8
9 /*
10 * 转换流,由字节流转换为字符流,字符流转换为字节流
11 * InputStreamReader和InputStreamWriter
12 */
13 public class Demo_10 {
14
15 public static void main(String[] args) throws Exception {
16 //将字节流转化为字符输入流
17 FileInputStream inbyte=new FileInputStream("D:\myjavacode\IO\src\abc.txt");
18 InputStreamReader in=new InputStreamReader(inbyte); //转换为字符流,in为字符流
19 BufferedReader read=new BufferedReader(in); //赋予缓冲区,提高效率,便于操作,有reader和writer最好都加上装饰缓冲流
20 read.readLine(); //可以执行字符操作
21
22 //再转换为字符输出流
23 FileOutputStream out=new FileOutputStream("D:/myjavacode/IO/src/abc1.txt");
24 OutputStreamWriter awrite=new OutputStreamWriter(out);
25 BufferedWriter write=new BufferedWriter(awrite);
26 String line=null;
27 while((line=read.readLine())!=null) {
28 write.write(line);
29 write.newLine();
30 write.flush();
31 }
32 write.close();
33 read.close();
34 }
35
36 }
数据流:DataOutputStream , DataInputStream
* 1、写出后读取
* 2、读取的顺序与写出保持一致
1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7
8 /**
9 * 数据流:DataOutputStream , DataInputStream
10 * 1、写出后读取
11 * 2、读取的顺序与写出保持一致
12 * */
13 public class Demo_11 {
14
15 public static void main(String[] args) throws Exception {
16 ByteArrayOutputStream baos =new ByteArrayOutputStream();
17 DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(baos));
18 //操作数据类型 +数据
19 dos.writeUTF("房廷飞");
20 dos.writeInt(18);
21 dos.writeBoolean(false);
22 dos.writeChar(‘a‘);
23 dos.flush();
24 byte[] datas =baos.toByteArray();
25 System.out.println(datas.length);
26 //读取
27 DataInputStream dis =new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
28 //顺序与写出一致
29 String msg = dis.readUTF();
30 int age = dis.readInt();
31 boolean flag = dis.readBoolean();
32 char ch = dis.readChar();
33 System.out.println(flag);
34 }
35
36 }
* 对象流:序列化和反序列化
1 import java.io.BufferedInputStream;
2 import java.io.BufferedOutputStream;
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectOutputStream;
10 import java.io.Serializable;
11 import java.util.Date;
12 /*
13 * 对象流:序列化和反序列化
14 */
15 public class Demo_12 {
16 public static void main(String[] args) throws IOException, ClassNotFoundException {
17 // 写出 -->序列化
18 ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.ser")));
19 /*往字节数组中写
20 ByteArrayOutputStream out = new ByteArrayOutputStream();
21 ObjectOutputStream oos2 = new ObjectOutputStream(new BufferedOutputStream(out));
22 */
23 // 操作数据类型 +数据
24 oos.writeUTF("编码辛酸泪");
25 oos.writeInt(18);
26 oos.writeBoolean(false);
27 oos.writeChar(‘a‘);
28 // 对象
29 oos.writeObject("谁解其中味");
30 oos.writeObject(new Date());
31 Employee emp = new Employee("房廷飞", 8000);
32 oos.writeObject(emp);
33 oos.flush();
34 oos.close();
35 // 读取 -->反序列化
36 ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.ser")));
37 /*从字节数组中取
38 ByteArrayInputStream in=new ByteArrayInputStream(out.toByteArray());
39 ObjectInputStream ois2 = new ObjectInputStream(new BufferedInputStream(in));
40 */
41 // 顺序与写出一致
42 String msg = ois.readUTF();
43 int age = ois.readInt();
44 boolean flag = ois.readBoolean();
45 char ch = ois.readChar();
46 System.out.println(flag);
47 // 对象的数据还原
48 Object str = ois.readObject();
49 Object date = ois.readObject();
50 Object employee = ois.readObject();
51
52 if (str instanceof String) {
53 String strObj = (String) str;
54 System.out.println(strObj);
55 }
56 if (date instanceof Date) {
57 Date dateObj = (Date) date;
58 System.out.println(dateObj);
59 }
60 if (employee instanceof Employee) {
61 Employee empObj = (Employee) employee;
62 System.out.println(empObj.getName() + "-->" + empObj.getSalary());
63 }
64 ois.close();
65 }
66 }
67 class Employee implements Serializable{
68 private int salary;
69 private String name;
70 public Employee(String name,int salary) {
71 super();
72 this.salary = salary;
73 this.name = name;
74 }
75 public int getSalary() {
76 return salary;
77 }
78 public void setAge(int age) {
79 this.salary = age;
80 }
81 public String getName() {
82 return name;
83 }
84 public void setName(String name) {
85 this.name = name;
86 }
87 }
以上是关于IO流——各种流整理的主要内容,如果未能解决你的问题,请参考以下文章
java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段
java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段