Byte stream and character stream in java i/o system

How to read the file content?
Through File objects, we can read the attribute information of files or folders. If we need to read the content data of files, then we need to use IO stream technology.
Read and write files through streams
Stream refers to a series of flowing characters. It is a first-in, first-out channel for sending messages.

Document classification
1. Text class files (txt, log, html, etc.), using character streams
2. Binary files (pictures, office documents, bak, database files using byte stream)

Byte stream:
The byte stream reads all the binary data in the file, and the read binary data will not undergo any processing.
Character stream:
Character streams read data in character units. It also reads the binary data in the file, but converts the binary data into characters that we can recognize.

Read file of byte stream

step

//Find the target file
File file = new File(path);
//Establishment of Data Transmission Channel
FileInputStream fis = new FileInputStream(file);
//Declare the read value of a variable store 
int content = 0;
//Reading data from a file using a loop
while((content = fis.read()) != -1);
//Use variables to save read data in the while loop
   /.....Save code...../
 //close resource
 fis.close

 class Input1{
    public static void inputi(){
        File file2 = new File("E:\\TextOutput\\Test002.txt");
        try {
            FileInputStream fileInputStream =new FileInputStream(file2);
            int t =  0;                                      
            
            while ((t = fileInputStream.read())!=-1){                
                System.out.print(t);
            }
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.print("File Reading Error");
        }
    }
}

Reading a byte at a time is slow

class Input1{
    public static void inputi(){
        File file2 = new File("E:\\TextOutput\\Test002.txt");
        try {
            FileInputStream fileInputStream =new FileInputStream(file2);
            int lenght=  0;    
            byte[] t = new byte[1024];      
            while ((lenght = fileInputStream.read(t))!=-1){                
                // Convert the byte array to a string because length is the actual read byte 
                String str = new String(buf, 0, length);
            }
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.print("File Reading Error");
        }
    }
}
//Read 1024 at a time

Output file of byte stream

class write{
public static void write() {
	File file = new File("E:\\TextOutput\\Test002.txt");
	FileOutputStream fileOutputStream;
	try {
		fileOutputStream = new FileOutputStream(file);
		String a = "adadasd";
		byte[] out=a.getBytes(); 
		try {
			fileOutputStream.write(out);
			fileOutputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}

Character stream

input

class Input{
   static String file1=null;
    public static void read1() throws IOException{
        File file = new File("E:\\TextOutput\\Test001.txt");
        //Find the target file
        FileReader fileReader =new FileReader(file);
        //Establishing Data Input Channel
        char[] buffer = new char[1024];
        //Establishment of CNC Buffer Zone
        int lenght = 0;

        //lenght = fileReader.read(buf);
        while ((lenght = fileReader.read(buffer)) > -1){
            String info =new String(buffer,0,lenght);
            file1 = file1+info;
            System.out.println(info);
        }

        fileReader.close();
    }
}

output

class Output{
    public static void output1(){
        File output = new File("E:\\TextOutput\\Test003.txt");
        try {
            FileWriter fileWriter = new FileWriter(output);
            char[] buffer = new char[1024];
            int lenght = Input.file1.length();
            fileWriter.write(Input.file1,0,lenght);
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Output error");
        }
    }

}

Tags: Attribute Database

Posted on Wed, 09 Oct 2019 08:27:57 -0700 by lobo235