Buffer Stream for the Use of IO Flows

Keywords: Java Android

The content of the article is summarized as follows:

Click on the text to jump

- What is Cache Stream
- Cache input stream and read files
- Cache output stream and write data
- Comprehensive drill, read A file, and write content to B file
- Pre-class Review (FileIn/OutputStream,FileWrite/Reader)
What? You are still confused about IO, so poke me to summarize some common examples of IO and learn some basic knowledge.~~

ps: This general summary of common examples is currently just hip-hopping during the creation phase (^____________)

What is File Cache Stream
File cache streams include: BufferReader and BufferWrite
Cache stream enhances the ability to write articles independently. It can read data in rows. It is a kind of character output stream and input stream.
Instance:
BufferReader br=new BufferReader(Reader in);
BufferWrite bw=new BufferWrite(Write out);
Common methods:
BufferReader: readerLine(); reads the contents of a line
BufferWrite:write(String s); write a string
newLine(); newline writing

Read File Content - BufferReader

String name="C:\\Users\\lenovo\\Desktop\\text\\c.txt";
        File file = new File(name);
//Read data
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            //Assign read data to string
            String string =null;//Read empty at the end of the file
            while((string=br.readLine())!=null){
                System.out.println(string);

            }
            //Closure from upper to lower laminar flow
            br.close();
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Write content to a file -- BufferReader

// Write data to a file
        String[] content = { "I love JAVA", "I love Android", "I love HTML" };
        String name="C:\\Users\\lenovo\\Desktop\\text\\c.txt";
        File file = new File(name);
        try {
            FileWriter fw = new FileWriter(file, true);// The second parameter, true append information, false override (override the contents of previous files)
            BufferedWriter bw = new BufferedWriter(fw);
            for (String a : content) {
                bw.write(a);// Write, one element at a time
                bw.newLine();// Line change
            }
            // The upper laminar flow closes slowly to the lower laminar flow
            bw.close();
            fw.close();
            System.out.println("Write successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }


Front High Energy: Practical Exercise!!!


First, we create two files c.txt,e.txt, which will read from C.
Write it to e, E. There's nothing in it. Here's the content of the c file.

//Read c file and write to e
        File fileC=new File("C:\\Users\\lenovo\\Desktop\\text\\c.txt");
        File fileE=new File("C:\\Users\\lenovo\\Desktop\\text\\e.txt");
        try {
            FileReader fr=new FileReader(fileC);
            FileWriter fw=new FileWriter(fileE);
            BufferedReader br=new BufferedReader(fr);
            BufferedWriter bw=new BufferedWriter(fw);
            //Read the data in C first
            String str=null;
            //Use the while loop to read each line and write while reading
            while((str=br.readLine())!=null){
                bw.write(str);
                //Don't forget the change of profession here.
                bw.newLine();

            }
            //Closing flow
            br.close();
            bw.close();
            fr.close();
            fw.close();
            System.out.println("Write successfully");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Note here that if you don't add bw.newLine(); the effect is as follows

No line change
That's what happens when you add it up.

Step summary:
1. Define two files to operate on
2. Instantiating FileReader/Writer classes
3. Instantiate cache stream objects by passing FileReader/Writer objects into BufferReader/Writer
4. Define a String string to store each read data
5. Use the while loop to read line by line and write the read data to the target file
6. Close the flow

That's all for today. Thank you for your reading. Best bye.~

Posted by kobayashi_one on Wed, 17 Jul 2019 11:55:34 -0700