io file

Superclass: 
Byte stream: InputStream (read in stream) OutputStream (write out stream) 
Character stream: Reader (character read in stream) Writer (character write out stream)

File operation stream 
byte stream: FileInputStream, FileOutputStream 
character stream: FileReader, FileWriter (the usage is basically the same as the byte stream, no writing)

        //1.指定要读 的文件目录及名称
        File file =new File("文件路径");
        //2.创建文件读入流对象 FileInputStream fis =new FileInputStream(file); //3.定义结束标志,可用字节数组读取 int i =0 ; while((i = fis.read())!=-1){ //i 就是从文件中读取的字节,读完后返回-1 } //4.关闭流 fis.close(); //5.处理异常
        //1.指定要写到的文件目录及名称
        File file =new File("文件路径");
        //2.创建文件读入流对象 FileOutputStream fos =new FileOutputStream(file); //3.定义结束标志 fos.write(要写出的字节或者字节数组); //4.刷新和关闭流 fos.flush(); fos.close(); //5.处理异常

Buffered Stream: 
Byte Buffered Stream: BufferedInputStream, BufferedOutputStream 
Character Buffered Stream: BufferedReader, BufferedWriter Buffered 
stream is the enhancement of the function of stream operations and improves the efficiency of data reading and writing. Since the buffered stream is the enhancement and improvement of the function of the stream and the efficiency of reading and writing, the stream object to be enhanced should be passed in when creating the object of the buffered stream.

//1.指定要读 的文件目录及名称
        File file =new File("文件路径");
        //2.创建文件读入流对象 FileInputStream fis =new FileInputStream(file); //3.创建缓冲流对象加强fis功能 BufferedInputStream bis =new BufferedInputStream(fis); //4.定义结束标志,可用字节数组读取 int i =0 ; while((i = bis.read())!=-1){ //i 就是从文件中读取的字节,读完后返回-1 } //5.关闭流 bis.close(); //6.处理异常
        //1.指定要写到的文件目录及名称
        File file =new File("文件路径");
        //2.创建文件读入流对象 FileOutputStream fos =new FileOutputStream(file); //3.创建缓冲流对象加强fos功能 BufferedOutputStream bos=new BufferedOutputStream(fos); //4.向流中写入数据 bos.write(要写出的字节或者字节数组); //5.刷新和关闭流 bos.flush(); bos.close(); //6.处理异常


It can be seen that the operation of the stream is basically the same. The operation of this stream is almost the same as that of the file stream, but the function of reading and writing files in the heap file stream is strengthened in the construction method of passing the file stream as a parameter to the buffer stream.  The buffered stream BufferedReader also provides a method for reading a line, readLine(), which can read a line of text and 
write out characters in the buffered stream. The BufferedWriter also provides a method for writing a line separator, writeLine(), which is used for line breaks when writing out 
. 2: The decoration (decoration) mode in the GoF design mode is used here. 
Note 3: The loading mode of the object that creates the buffer stream:

BufferedInputStream bis =new BufferedInputStream(
new FileInputStream(new File("文件路径"))); 

Note 4: The buffer stream parameter only needs to be a subclass of a superclass

object stream 

ObjectInputStream and ObjectOutputStream 
are different from the above two types of streams. Here, only bytes can be used to operate objects. The reason can be seen in the coding table comparison principle in the previous article.

Serialization of ObjectOutputStream objects: 
write the objects in the java program to the local disk with ObjectOutputStream 
eg: serialize the objects of the Person class to disk

1.创建Person类
    注1:此类要实现Serializable接口,此接口为标志性接口
    注2:此类要有无参的构造函数
    注3:一旦序列化此类不能再修改 class Person implements Serializable{ public Person(){} } 2.创建对象流对象 注:要增强功能可以将传入文件缓冲流 ObjectOutputStream oos =new ObjectOutputStream( new FileOutputStream(new File("文件路径"))); 3.写入对象 ,一般会将对象用集合存储起来然后直接将集合写入文件 List<Person> list =new ArrayList<>(); list.add(new Person()); ...(可以添加多个) oos.writeObject(list); 4.关闭流,处理异常 oos.flush(); oos.close();

Deserialization of ObjectInputStream objects: 
use ObjectInputStream to read the object file from the local disk into the java program

1:用集合接收的时候要强转
1.创建对象流对象
    ObjectInputStream ois =new ObjectInputStream(
    new FileInputStream(new File("文件路径"))); 2.读入对象 List<Person> list =new ArrayList<>(); List =(List<Person>)ois.readObject(); 3.关闭流,处理异常 ois.close();

Conversion stream: 
This type of stream is used to convert characters into byte input and output, and is used to operate character files. It is a subclass of character stream, so the suffix is ​​reader, writer; prefix inputstream, outputstream; Note: To pass in bytes Stream as contest 
InputStreamReader: character conversion output stream 
OutputStreamWriter: character conversion input stream

需求:读取键盘输入的一行文本,再将输入的写到本地磁盘上
//1.获取键盘输入的字节流对象in
InputStream in =Stream.in; /*2.用转换流将字节流对象转换为字符流对象,方便调用字符缓冲流的readeLine()方法*/ InputStreamReader isr =new InputStreamReader(in); /*5.创建字符转换输出流对象osw,方便把输入的字符流转换为字节输出到本地文件。*/ OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream(new File("文件名"))); /*3.现在isr是字符流,可以作为参数传入字符缓冲流中*/ BufferedReader br =new BufferedReader(isr); /*4.可以调用字符缓冲流br的readLine()方法度一行输入文本*/ String line =null; while((line =br.readLine()){ osw.write(line);//osw是字符流对象,可以直接操作字符串 } 注:InputStreamReader isr =new InputStreamReader(new "各种类型的字节输入流都行即是:后缀为InputStream就行"); OutputStreamWriter osw =new OutputStreamWriter(new "后缀为OutputStream就行"); 

Difference between memory 
1. The object stream can read and write almost all types as long as it is an object, while the byte character stream can only read and write a single byte character or byte character array. The above does not read and write byte character arrays; note Object streams are only byte streams! 
2. End condition of character and byte loop read int i=0; (i =fis.read())!=-1 
Copy file with character array (fr reads in stream, fw writes out stream), byte stream also the same usage

  int i = 0;
  char[] c = new char[1024]; while((i = fr.reade()) !=-1)){ fw.write(c,0,i); } 

3. The scene where the buffer stream is set in the object stream: 

new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(“文件路径”))));

4.记忆流及其功能的方法: 
前缀表示功能,后缀表示流的类型; 
比如说FileInputStream 前缀:File,表示操作的磁盘,后缀:intputstream,表示是字节输入流。 
同理 FileReader:表示操作文件的字符流 
ObjectInputStream :操作对象的字节输入流

5.拓展:获取键盘输入的字符的缓冲流的写法: 
new BufferedReader(new InputStreamReader(System.in))); 
将字节以字符形式输出到控制台的字符缓冲流的写法: 
new BufferedWriter( new OutputStreamWriter(System.out))

Guess you like

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