When we read and write files, we use memory as a reference

Output: If the data in memory is saved on the hard disk

Input: If data is read from a hard disk into memory

File read and write related classes: InputStream and OutputStream. Both of these classes are abstract and cannot be used directly. This stream can also be called a bystream and subclasses FileInputStream and FileOutputStream are used

Inheritance relationship:

1.FileOutputStream

Sample code:

1. Create a stream object. If true is specified as the second argument to the constructor, concatenating data is allowed

2. Convert the character string to a character array and write it to the hard disk

Write data to hard disk:

Public class TestWrite {public static void main(String[] args) throws Exception {// Creates a stream object if specified as the second constructor argumenttrueFileOutputStream fos = new FileOutputStream(new File("d:\\world.txt"),true); FileOutputStream fos = new FileOutputStream("d:\\world2.txt".true); // Prepare data. String s ="Good afternoon"; Byte [] bytes = s.gettbytes (); // Write data to the hard disk fos.write(bytes); // Close the resource fos.close(); }}Copy the code

2.FileInputStream

Inheritance relationships

Reads content from a file onto hard disk

Public class TestRead {public static void main(String[] args) throws Exception {FileInputStream fis = new FileInputStream("d:\\world.txt");
		int r = 0;
		while(1! Print ((char)r); print(char)r); print(char)r); print(char)r); } fis.close(); }}Copy the code

3. Byte stream copy files

This method performs particularly poorly, reading and writing one byte at a time

Public class TestCopy1 {public static void main(String[] args) throws Exception {FileInputStream fis = new FileInputStream("d:\\w.jpg");
		FileOutputStream fos = new FileOutputStream("c:\\w.jpg");
		int r = 0;
		while(1! = (r = fis.read())) { fos.write(r); } // fos.close(); fis.close(); }}Copy the code

Conversion knowledge of popular science units:

The smallest unit of a file is the byte

1024 bytes = 1 KB

1024kb = 1mb

1024mb = 1G

1024g = 1T

Quick copy of any file :(interview question)

Save what you need to read and write each time in a byte array, and each time you read the contents of the byte array, you will greatly speed up the replication rate, the best effect! The buffer stream also stores data internally through arrays, so this method is the most efficient

Public class TestCopy3 {public static void main(String[] args) throws Exception {FileInputStream fis = new FileInputStream("d:\\javatool.zip");
		FileOutputStream fos = new FileOutputStream("c:\\javatool.zip");
		byte[] buf = new byte[1024];
		int len = 0;
		while(1! = (len = fis.read(buf))) { fos.write(buf, 0, len); } fos.close(); fis.close(); }}Copy the code

4. The buffer flow

We refer to the flows we deal with as filtered flows

We refer to flows that work directly with files as node flows

Inheritance relationships

Buffer stream is also in the form of byte array for copy operation, so it is the most efficient to directly define byte array to store elements. Buffer byte input stream and buffer byte output stream also need to be internally converted with array, so the efficiency is slightly lower

Public class TestCopy4 {public static void main(String[] args) throws Exception {// We call streams that deal with streams as filter streams Bis = new BufferedInputStream(new FileInputStream())"d:\\javatool.zip"), 1024); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\javatool.zip"), 1024); int i = 0;while(1! = (i = bis.read())){ bos.write(i); } bos.close(); Bis.close (); // When a filter stream is closed, bis.close() is closed for all streams. }}Copy the code

5. Resolve garbled characters

Code table

  • 1.ASCII can only store English characters A-> 97 A->65
  • 2. Iso-8859-1 encoding, including not only English characters, but also Latin characters
  • 3.GB2312 code contains only simplified characters
  • GBK includes both simplified and traditional characters
  • 5.Unicode encoding, which contains characters from more than 650 countries
  • Utf-8, a compressed version of Unicode

Chinese characters take up 2 bytes if they are encoded in GBK and 3 bytes if they are encoded in UTF-8

The Windows system uses GBK encoding by default, so a Chinese character takes up two bytes. If two bytes are read at a time, there is no garbled code

Read two bytes at a time, without garbled characters, simulating character stream:

public class TestRead {
	public static void main(String[] args) throws Exception {
		FileInputStream fis = new FileInputStream("d:\\world.txt");
		byte[] buf = new byte[2];
		int len = 0;
		while(1! = (len = fis.read(buf))) { String s = new String(buf, 0, len); System.out.println(s); } fis.close(); }}Copy the code

6. Exception handling in I/O

Handling the exception:

  • 1. Delete throws Exception
  • In addition to closing resources, surround with try… catch
  • 3. Set the extracted variable to NULL
  • 4. Merge exceptions
  • 5. Exception conversion
  • 6. Add a finally block and put the code to close the resource in that block
  • 7. Perform a non-null check on closed objects
  • 8. Handle the closed resource exception

Sample code:

Public class TestCopy1 {public static void main(String[] args) {// Handle the exception step //1. Remove throws Exception //2. Select the surround with try code in addition to closing resources. Catch //3. Assign the extracted variable to null //4. Merge exception //5. Exception conversion //6. Add a finally block and put the code to close the resource in that block //7. FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("d:\\w.jpg");
			fos = new FileOutputStream("c:\\w.jpg");
			int r = 0;
			while(1! = (r = fis.read())) { fos.write(r); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { try {if(null ! = fos) { fos.close(); }if(null ! = fis) { fis.close(); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); }}}}Copy the code

Io byte stream is introduced so much, the other method of byte stream operation is not quite skilled small cute people, you can see the official API documentation

Remember to like + follow 👉 : my github address: github.com/Lmobject