1.IO
1.1 general
Stream is a set of sequential bytes with starting point and ending point. It is the general name or abstraction of data transmission. That is, the transmission of data between two devices is called flow. The essence of flow is data transmission. According to the characteristics of data transmission, the flow is abstracted into various types to facilitate more intuitive data operation.
I: input: input data into memory
O: output: write out the data in memory
1.2 classification
- It is divided into byte stream and character stream according to different processing data types
-
According to the different data flow direction, it is divided into input flow and output flow. (in and out are relative to memory)
-
According to different functions, it is divided into node flow and processing flow
Node flow: direct manipulation of data sources
Process flow: process other flows
1.3 four abstract classes
InputStream: byte input
OutputStream: byte output
Reader: character input
Writer: character output
1.4 InputStream
FileInputStream is a byte input stream that reads data from a file
Operating system level Everything is a document
1.4.1 read usage
Read: read the data in the file, one byte at a time. The return value is the value of the read byte (return int type). If it is read to the end of the file (after reading), it returns - 1
public static void main(String[] args) throws Exception { //To read a file, you must find it //How to find it? Two methods: absolute path and relative path //Absolute path: the full path of this file can be found //Relative path: find other files relative to the location of the current file,. / indicates the current directory,.. / indicates the last directory,.. /.. / indicates the upper level directory //FileInputStream fis=new FileInputStream("C:\java\a.txt"); \ Or/ //. / found in eclipse is the current project FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01.java"); //Read: read the data in the file, one byte at a time. The return value is the value of the read byte (return int type). If it is read to the end of the file (after reading), it returns - 1 int i1=fis.read(); System.out.println((char)i1); }
Cyclic reading
public static void main(String[] args) { try(FileInputStream fis=new FileInputStream("./src/com/IO_01_FileInputStream_01.java")) { int temp=0; while((temp=fis.read())!=-1){ System.out.print((char)temp); } } catch (Exception e) { e.printStackTrace(); } }
1.4.2 read overload usage
The overload of the read method can pass a byte array in order to improve the reading efficiency
By default, one byte is read at a time. If a byte array is passed, the array will be read full at one time, and then come back, and then read full again next time until it is finished
read(byte []): read an array one at a time, and return the number read at the current time. If it reaches the end of the file, return - 1
public static void main(String[] args) { try(FileInputStream fis =new FileInputStream("./src/com/IO_01_FileInputStream_01.java")) { System.out.println(fis.available()); //available: the number of bytes that can be read // byte[] bytes = new byte[fis.available()]; byte[] bytes=new byte[1027]; int count=fis.read(bytes); System.out.println(new String(bytes,0,count)); System.out.println(count); //new String(bytes) converts all data in its array into strings, but there may be redundant data // Therefore, it is recommended to use new String(bytes,0,count) to only convert the data read this time into a string count=fis.read(bytes); System.out.println(new String(bytes,0,count)); System.out.println(count); } catch (Exception e) { e.printStackTrace(); } }
Cyclic reading
public static void main(String[] args) { try(FileInputStream fis=new FileInputStream("./src/com/IO_01_FileInputStream_01.java")) { byte[] bytes=new byte[1025]; int count=0; while((count=fis.read(bytes))!=-1){ System.out.print(new String(bytes,0,count)); } } catch (Exception e) { e.printStackTrace(); } }
1.5 Reader
FileReader character input stream, byte input stream reads one byte at a time, while character input stream reads one character at a time
Moreover, Chinese characters in java are encoded in unicode, which occupies 16 bits, that is, one character
Therefore, using character input stream can solve the problem of Chinese character scrambling
Character stream is generally only used for plain text files, such as compressed packages, pictures, videos, etc. byte stream processing is still required
read(): read a character, return the int value of the character, and return - 1 at the end of the file
read(char []): reads a char array, returns the current number of reads, and returns - 1 at the end
1.5.1 read usage
try (FileReader fr = new FileReader("./src/com/IO_01_FileInputStream_01.java")) { int temp = 0; while ((temp = fr.read()) != -1) { System.out.print((char) temp); } } catch (Exception e) { e.printStackTrace(); }
1.5.2 read overload usage
try (FileReader fr = new FileReader("./src/com/IO_01_FileInputStream_01.java")) { char[] chars = new char[512]; int temp = 0; while ((temp = fr.read(chars)) != -1) { System.out.println(new String(chars, 0, temp)); } } catch (Exception e) { e.printStackTrace(); }
1.6 OutputStream
1.6.1 concept
The output stream writes out the data in memory to the hard disk
If the file does not exist, it is created automatically, but the folder is not created
If the corresponding file directory does not exist, an error is reported
Construction method
File OutputStream (string path): write out data to the file and overwrite the original content
FileOutputStream(String path,boolean append): write out data to the file. If append is true, write out will be appended, and if false, overwrite
write(int): write out a data
write(byte[] b): write out all contents of the array
write(byte[] b, int off,int len): write out the specified data in the array
flush(): brush the cache. It can be called without calling. It will be called automatically when the flow is closed
Auxiliary method: getBytes(): the method in String, which is used to obtain the byte array corresponding to the String
1.6.2 mode of use
public static void main(String[] args) { try(FileOutputStream fos=new FileOutputStream("c:/java/a.txt",true)) { fos.write(97); fos.write(98); fos.write(99); fos.write(100); fos.write('\n'); String str="how are you"; byte[] bytes=str.getBytes(); fos.write(bytes); fos.flush(); } catch (Exception e) { e.printStackTrace(); } }
1.7 Writer
1.7.1 concept
1.7.2 mode of use
public static void main(String[] args) { try(FileWriter fw=new FileWriter("C:/java/a.txt");) { fw.write("how are you?\n"); //Write out string char[] chars={'a','b','c','d'}; fw.write(chars); //Write out char array fw.flush(); } catch (Exception e) { e.printStackTrace(); } }
1.8 buffer flow
1.8.1 concept
1.8.2 BufferedInputStream
long startTime = System.currentTimeMillis(); try (FileInputStream fis = new FileInputStream("C:/java/b.txt"); BufferedInputStream bis = new BufferedInputStream(fis);) { byte[] bytes = new byte[1024]; int temp = 0; while ((temp = fis.read(bytes)) != -1) { // System.out.println(new String(bytes,0,temp)); } long endTime = System.currentTimeMillis(); System.out.println("Read complete,time consuming : " + (endTime - startTime)); } catch (Exception e) { e.printStackTrace(); }
1.8.3 BufferedOutputStream
public static void main(String[] args) { try (FileOutputStream fos = new FileOutputStream("C:/java/a.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); ) { bos.write(97); bos.write(98); bos.write(99); bos.write(100); bos.write('\n'); String str = "how are you"; byte[] bytes = str.getBytes(); bos.write(bytes); bos.flush(); } catch (Exception e) { e.printStackTrace(); } }
1.8.4 BufferedReader
New method String readLine(): reads a line of data, the return value is the read data, and returns null at the end of the file
public static void main(String[] args) { try (FileReader fr = new FileReader("C:/java/a.txt"); BufferedReader br = new BufferedReader(fr)) { String temp=null; while((temp=br.readLine())!=null){ //Read one line System.out.println(temp); } } catch (Exception e) { e.printStackTrace(); } }
1.8.5 BufferedWriter
public static void main(String[] args) { BufferedWriter bw=null; try { bw=new BufferedWriter(new FileWriter("C:/java/a.txt")); bw.write("how are you"); bw.newLine(); //Line feed bw.write("I'm fine"); bw.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(bw!=null){ bw.close(); } } catch (IOException e) { e.printStackTrace(); } } }