Java中IO流之FileOutputStream、FileIntputStream、FileReader 、FileWriter、ObjectOutputStream

Summary of IO stream learning in Java

1. The structure in the IO stream

io stream picture

  • Character stream: As the name suggests, this stream can only handle characters, but it is fast
  • Byte stream: It can process all files stored in bit units, that is to say, it can process all files, but it is not as fast as character streams in processing characters

2. The specific use of IO stream

  • From various input streams to various output streams 
    Note: In fact, in different types, the routines from input streams to output streams are basically the same. 
    Then take the simplest FileOutputStream as an example. 
    From FileOutputStream to FileIntputStream is actually the process of copying a file, reading the file into FileIntputStream, and then outputting it to FileOutputStream, which is equivalent to outputting to a file on the hard disk. 
    We can take two buckets as an example, one bucket is FileInputStream, and the other bucket is FileOutputStream. If we want to transfer the water in one bucket to another bucket, we first need a water scoop, and scooping water again and again can complete our demand. 
    Without further ado, let’s go directly to the code:
public static void main(String[] args) throws IOException {
        File fil1 = new File("D:/111.pdf");
        File fil2 = new File("D:/222.pdf");
        try (FileInputStream fi = new FileInputStream(fil1); 
        //一个叫输入流的桶,装满了一桶叫做D:/111.pdf文件的水
        FileOutputStream fs = new FileOutputStream(fil2);
        //一个叫输出流的空桶,但想装满叫做"D:/222.pdf"文件的水
                ) {
            byte[] buf = new byte[521];
            //叫做buf的水瓢
            int len = -1;
            //用来测量每次水瓢装了多少水
            while((len = fi.read(buf)) != -1){
            //一次次的用水瓢在输入流的桶里舀水,并用len测了舀了多少水,当len等于-1意味着水舀光了,该结束舀水了。
                fs.write(buf, 0, len);
                //一次次把水瓢里的水放到了输出流的桶里
            }
            fs.flush();
        } catch (Exception e) {
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

In fact, this method can be used for many input streams and output streams. 
-From  input stream to string 
In fact, this is very similar to the previous one, but it is implemented in a different way. 
Go directly to the code:

        File file = new File("D:/123.txt");
        FileInputStream fis = new FileInputStream(file);
        //同样是叫做输入流的桶
        StringBuffer sb = new StringBuffer();
        //把输出流的桶换成了StringBuffer用来储存字符串
        //其实也可以直接用String,但是StringBuffer速度更快。
        byte[] buf = new byte[256];
        //水瓢没变
        int len = -1;
        //测水瓢舀了多少水没变
        while ((len = fis.read(buf)) != -1){
            sb.append(new String(buf, 0, buf.length));
            //和上面的原理基本一样,只不过换了个水瓢而已
            //new String(buf, 0, buf.length)是将buf里面的内容转换为字符串
        }
        System.out.println(sb.toString());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • Using the character stream to write a string to a file or read a string from a file is actually the same as the previous byte stream reading and writing idea, but it restricts the file to only character type. 

Copy text file and output


public static void main(String[] args) throws Exception {
        File file = new File("D:/123.txt");
        //复制源文件
        File file2 = new File("D:/456.txt");
        //复制结果文件
        StringBuffer sb = new StringBuffer();
        //用于输出到控制台
        if(!file2.exists()){
            file2.createNewFile();
        }
        //检测结果文件是否存在如果不存在便创建一个
        FileReader fr = new FileReader(file);
        //设置字符读入流用于向文件(file)中读数据
        FileWriter fw = new FileWriter(file2);
        //设置字符读出流用于向文件(file2)中写数据
        char[] ch = new char[256];
        //每次读和写的容器,或者说是传送的媒介
        int len = -1;
        while((len = fr.read(ch)) != -1){
            fw.write(ch, 0, ch.length);
            //将容器里的东西写入到新文件中
            sb.append(new String(ch, 0, ch.length));
            //将容器里的东西添加到strngBuffer中,用于输出
        }
        fw.flush();
        fw.close();
        fr.close();
        System.out.println(sb.toString());
        //输出文本文件
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
    Use tools  for serialization and deserialization of objects
    : ObjectOutputStream, ObjectInputStream Introduction: save the object in the form of a file on the hard disk, so that it can be transmitted more conveniently. 
    Condition: Serializable interface must be implemented (this interface is implemented, but no method needs to be overridden)

Code:


class DemoObject implements Serializable{
    int date = 23; 
}

public class IoTest {
    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:/123.obj")));
        //建立对象输出流准备向文件中写入对象
        oos.writeObject(new DemoObject());
        //向文件中写入新建立的对象
        oos.flush();
        //输出流记得要flush
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:/123.obj")));
        //建立对象输入流准备在文件中读出刚写入的对象
        DemoObject newObject = (DemoObject)ois.readObject();
        //建立一个新对象用于保存刚刚读出的对象
        System.out.println(newObject.date);
        //输出这个对象
    }
}
Reprinted in: https://blog.csdn.net/Yue_Chen/article/details/72772445

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733643&siteId=291194637