This is the 10th day of my participation in Gwen Challenge

File stream

  • Byte stream: FileIntputStream, FileOutputStream
  • Character streams: FileReader, FileWriter
  1. For text files (.txt,.java,.c), character streams are used
  2. For non-text files (.jpg,.mp3,.mp4,.avi,.doc,.ppt), use byte stream processing

File byte stream

So TXT file

package FileInputOutputTest;

import java.io.*;
import java.io.IOException;

public class FileInputStreamTest {
    public static void main(String[] args)  {
        FileInputStream fis =null;
        try {
            / / 1. The file
            File file = new File("hello.txt");

            / / 2. The flow
            fis = new FileInputStream(file);

            / / 3. Read the data
            byte[] buffer = new byte[5];
            int len;// Record the number of bytes read each time
            while((len = fis.read(buffer)) ! = -1) {
                String str = new String(buffer, 0, len); System.out.print(str); }}catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4. Close the resource
            if(fis ! =null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

Exporting PNG files

package FileInputOutputTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileInputStreamTest2 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos =null;

        try {

            File srcfile = new File("Long live the monster. PNG");
            File destfile = new File("Long live the monster 1.png");

             fis = new FileInputStream(srcfile);
             fos = new FileOutputStream(destfile);

            byte[] buffer = new byte[5];
            int len;
            while((len = fis.read(buffer)) ! = -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("Copy successful!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos ! =null) {
                try {
                    fos.close();
                } catch(IOException e) { e.printStackTrace(); }}if(fis ! =null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

File character stream

  1. Read () : Returns a character read in. If you reach the end of the query, return -1
  2. Exception handling: To ensure that the resource can be shut down. A try-catch-finally is required
  3. The read file must exist or FileNotFoundException will be reported.

The input stream

1. The way a

public class FileReaderTest1 {
    public static void main(String[] args)  {
        FileReader fr =null;
        try {
        /* Read the contents of the hello. TXT file from the package into the program and print it to the console */
            //1. Instantiate an object of class File, specifying the File to operate on
            File file = new File("hello.txt");
            //2. Provide a specific stream
            fr = new FileReader(file);
            //3. The process of data reading
            //read(): returns a character read in. If the end of the file is reached, -1 is returned
// int data=fr.read();
// while (data ! = 1) {
// System.out.print((char) data);
// data=fr.read();
/ /}
            // Method 2:
            int data;
            while((data = fr.read()) ! = -1) { System.out.print((char) data); }}catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4. Close the stream
            try {
                if(fr ! =null) { fr.close(); }}catch(IOException e) { e.printStackTrace(); }}}}Copy the code

2. 2

package FileReaderWriterTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest2 {
    public static void main(String[] args)  {
        // Update to the read () operation: use the overloaded method of read
        FileReader fr=null;
        try {

            //1. Instantiate the File class
            File file = new File("hello.txt");

            //2. Instantiate the FileReader stream
             fr = new FileReader(file);

            //3. Read in
            //read(char[] cbuf): returns the number of characters in each cbuF array read. If the end of the file is reached, -1 is returned
            char[] cbuf = new char[5];
            int len;
// while ((len = fr.read(cbuf)) ! = 1) {
// for (int i = 0; i 
// System.out.print(cbuf[i]);
/ /}
/ /}
// System.out.println();
            2 / / way
            while((len = fr.read(cbuf)) ! = -1) {
                String str = new String(cbuf, 0, len); System.out.print(str); }}catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4. Resource shutdown
            if(fr ! =null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        }
}

Copy the code

The output stream

  1. Output operation, the corresponding File may not exist. No exceptions are reported

  2. If not, the file will be created automatically during output.

    If there is,

    If the stream uses FileWriter(file, false) /FileWriter(file): Overwrites the original file

    If the stream uses a FileWriter constructor (file, true) : Appends content to the original file

package FileReaderWriterTest;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderWriterTest {
    public static void main(String[] args) {
        FileWriter fw = null;
        FileReader fr = null;
        try {
            //1. Create an object of class File that specifies which files to read and write
            File srcFile = new File("hello.txt");
            File destFile = new File("hello1.txt");

            // Character streams cannot be used to process byte data such as images
// File srcFile = new File(" long live monster. PNG ");
// File destFile = new File(" long live 1.png");
            //2. Create objects for input streams and output streams
             fr = new FileReader(srcFile);
             fw = new FileWriter(destFile);

            //3. Read and write data
            char[] cbuf = new char[5];
            int len;// Records the number of characters read into the cBUF array each time
            while((len = fr.read(cbuf)) ! = -1) {
                fw.write(cbuf, 0, len);// Write len characters at a time}}catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4. Close the resource
                try {
                    if(fw ! =null) { fw.close(); }}catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fr ! =null) {
                        try {
                            fr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

        }

    }
}
Copy the code

Writes data from memory to a hard disk file

package FileReaderWriterTest;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterTest {
    public static void main(String[] args) throws IOException {
        /* Write data from memory to disk file */

        //1. Provide File
        File file = new File("hello.txt");

        //2. Provide the FileWriter object for writing data
        FileWriter fw = new FileWriter(file);

        //3. Write operations
        fw.write("I have a dream! \n");
        fw.write("you need have a dream ,too.");

        //4. Close the stream resourcefw.close(); }}Copy the code