当前位置:网站首页>_ 19_ IO stream summary

_ 19_ IO stream summary

2022-06-25 16:20:00 weixin_ forty-eight million six hundred and forty-four thousand

1 IO

summary : A stream is an ordered set of bytes from beginning to end , Is the general term or abstraction of data transmission . The transmission of data between two devices is called streaming , The essence of stream is data transmission , According to the characteristics of data transmission, streams are abstracted into various kinds

I : input Input

O : output Output

1.2 classification

It is divided into byte stream and character stream by data type

Different by data flow , It is divided into input stream and output stream .

According to different functions , It is divided into node flow and processing flow

Node flow : Direct manipulation of data sources

Processing flow : Process other streams

1.3 Four abstract classes

Byte stream Character stream
Input stream InputStreamReader
Output stream OutputStreamWriter

1.3.1

InputStream

Common methods

void close() : Close this output stream and release all system resources that are not related to it

void flush() : Flushes this output stream and forces all buffered output bytes to be written

void write(byte[] b) : take b.length Bytes are written to this input stream from the specified byte array

void write(byte[] b,int off, int len) : Removes the specified byte array from the offset off At the beginning len Bytes are written to this input stream

abstract void write(int b) : Writes the specified bytes to this input stream

      1. Reader
      2.  Writer

    1.   File stream
      1.  FileInputStream
        1.   summary

Used to open the file and read the data in the file

Want to read a file , You have to find it

1 Absolute position

The system root directory shall prevail , such as D:/xxx\\xxx\xx\a.txt

2 The relative position

./ Represents the current directory

../ Indicates the parent directory

../../ Go to the superior directory

  Common methods

 Read Use

public static void main(String[] args) {
		//  Create byte input stream 
		//  Incoming file path , It is used to open this file to get data 
		//  Absolute path 
		// FileInputStream fis = new FileInputStream("D:\\a.txt");
		//  stay eclipse in ,./ Locate the current project , Not the current file 
		//  Relative paths 
		try {
			FileInputStream fis = new FileInputStream("./src/b.txt");
			// read :  Read a byte , And return the corresponding ASCII Code value , Return to int type , If you reach the end of the file ( After reading the )  Then return to -1
			// int data = fis.read();
			int data = 0;
			while ((data = fis.read()) != -1) {
				System.out.print ((char) data);
			}
			// data = fis.read();
			// while (data != -1) {
			// System.out.print((char) data);
			// data = fis.read();
			// }
			//  close resource 
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

public static void main(String[] args) {
		//  Create byte input stream 
		FileInputStream fis = null;
		try {
			 fis = new FileInputStream("./src/b.txt");
			// read :  Read a byte , And back to , Return to int type , If you reach the end of the file ( After reading the )  Then return to -1
			// int data = fis.read();
			int data = 0;
			while ((data = fis.read()) != -1) {
				System.out.print ((char) data);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

read : Read a byte , And return the corresponding ASCII Code value , Return to int type , If you reach the end of the file ( After reading the ) Then return to -1

​
public static void main(String[] args) {
		try(FileInputStream fis = new FileInputStream("./src/b.txt")) {
			// available :  The number of bytes that can be read 
			System.out.println(fis.available());
			//  The larger the array capacity, the higher the efficiency , When the capacity and data size are just the same , Highest efficiency 
			byte[] bytes = new byte[fis.available()];
			int temp = 0;
			while (( temp = fis.read(bytes)) != -1) {
				//  Convert byte array to string , Data duplication may occur 
				// System.out.print(new String(bytes));
				//  because read Returns the number of elements currently read , So we can specify how much to read , How much to convert 
				//  Array  ,  The starting position ( contain ) ,  Number 
				 System.out.print(new String(bytes,0,temp));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

​

 Read Overload use

FileReader  summary

FileReader : Read one character at a time , That's two bytes , It is mainly used to read plain text files , Solve the mess
 * 
 * read() : Read one character at a time , Return the corresponding ASCII code , When the end of the file is reached, return -1
 * 
 * read(char[]) : Read one character array at a time , Improve reading efficiency , Returns the number of characters read locally , Return at the end of the file -1

  Usage mode

public static void main(String[] args) {
		try (FileReader fr = new FileReader("./src/b.txt");){
			int temp = 0;
			while ((temp = fr.read()) != -1) {
				System.out.print((char)temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
public static void main(String[] args) {
		try (FileReader fr = new FileReader("./src/b.txt");){
			int temp = 0;
			char[] chars = new char[10];
			while ((temp = fr.read(chars)) != -1) {
				System.out.print(new String(chars,0,temp));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

  1.  FileOutputStream
    1.   summary

  Common methods

  Construction method

public static void main(String[] args) {
		try (FileOutputStream fos = new FileOutputStream("D:/a.txt");) {
			//  Write the corresponding ASCII code 
			fos.write(97);
			byte[] bytes = {97,98,99};
			//  Write out array 
			fos.write(bytes);
			
			//  You cannot write out a string directly 
			String str = " how are you ";
			// getBytes :  Convert the string to a byte array 
			fos.write(str.getBytes());
			//  Refresh cache , Force persistence 
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

  Usage mode

public static void main(String[] args) throws IOException {
		//  Overwrite write , When creating the output stream object of the file , The contents of the file will be cleared 
		// FileOutputStream fos1 = new FileOutputStream("D:/a.txt");
		
		//  When you create an object , Pass in the second parameter  true  Description is append write , The file will not be emptied 
		FileOutputStream fos1 = new FileOutputStream("D:/a.txt",true);
		fos1.write(98);
	 
	}

 FileWriter

public static void main(String[] args) {
		//  Usage and byte output   Agreement    It's just a new method overload that can write out strings 
		try (FileWriter fw = new FileWriter("D:/a.txt")){
			//  You can write the data in the character array each time 
			char[] chars = {'a','c'};
			fw.write(chars);
			
			//  You can write out the string directly 
			fw.write(" how are you ?");
			
			fw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

  Buffer flow

【 characteristic 】

  1. It exists mainly to improve efficiency , Reduce the number of physical reads
  2. Provide readLine()、newLine() Such a convenient way ( For buffered character stream )
  3. When reading and writing , There will be a cache part , call flush To refresh the cache , Write memory data to disk

 BufferedReader

public static void main(String[] args) {
		try (
		//  Create character input stream 
		FileReader fr = new FileReader("./src/day_01/Test_01.java");
				//  Create character input buffer stream object 
				BufferedReader br = new BufferedReader(fr);) {
			// br.readLine() :  Read a row of data , Return the contents of the read row of data , by String ,  Return at the end of the file  null
			String temp = null;
			while ((temp = br.readLine()) != null) {
				System.out.println(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 BufferedWriter

public static void main(String[] args) {
		try (
		//  Create character output stream 
		FileWriter fw = new FileWriter("D:/a.txt");
				//  Create character output buffer stream 
				BufferedWriter bw = new BufferedWriter(fw);) {
			//  Write 
			bw.write(" how are you ");
			//  Line break 
			bw.newLine();
			bw.write(" Can you manage it ");
			//  Brush cache 
			bw.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

原网站

版权声明
本文为[weixin_ forty-eight million six hundred and forty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190534174119.html