Two sentences of forced percussion
Faster ~Happy ~
20-01: Overview and classification of IO streams
concept
IO streams are used to process data transfers between devices
Java manipulates data through streaming
Java classes for operation streams are in the IO package
Flow is divided into input flow and output flow by flow direction.
-
Streams are classified by operation type:
Byte stream: A byte stream can manipulate any data because any data on the computer is stored in bytes.
Character stream: Character stream can only operate on pure character data, which is more convenient.
Common parent of IO streams
-
Abstract parent of byte stream
InputStream
OutputStream
-
Abstract parent of character stream
Reader
Writer
-
IO Program Writing
Import classes in IO packages before use
IO exception handling when in use [IO streaming handles data transfer between memory and hard disk, which may not have specified files]
Release resources after use
20-(02-03): FileInputStream
The end tag of the file is -1
read() reads one byte at a time and returns the type int.
read file
package com.test.demo001; import java.io.FileInputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("yyy.txt"); int a; while((a = fis.read())!=-1){ System.out.println(a); //Code table values read } fis.close(); } }
Why is the return value of the read() method int
Because the byte input stream can operate on any type of file, such as audio and video, the bottom of these files are stored in binary form. If byte is returned every time it is read, 11111111 may be encountered in the middle. This 11111 is -1 in the byte type. Because Java programs stop reading when they encounter -1, all subsequent data will be unreadable.When reading, it is received with the int type. If 11111111 prefaces it with 24 zeros or 4 bytes, then -1 of the byte type becomes 255 of the int type, which ensures that the entire data is read, while -1 of the end tag is the int type.[When a file is written with write, the first three 8 bits are also removed to ensure data integrity]
20-(04-05): FileOutputStream
Although the write method writes out an int number, the first three eight bits of byte s are removed from the file.
package com.test.demo001; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("cccccc.txt");//Create an output stream object, or create one if no file is specified fos.write(97); //Although an int is written out, the first three eight bits of byte are removed from the file fos.write(98); fos.write(99); fos.close(); } }
When an output stream object is created, a file is created if it is not specified, and the contents of that file are emptied if there is one.
If you want to continue without empty content, do the following:
FileOutputStream fos = new FileOutputStream("yyy.txt",true); fos.write(97); fos.write(98);
20-(06-10): Copy
Three methods of copying
-
Byte-by-byte copy [inefficient, not recommended for development]
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("20.19_day20 summary.avi"); FileOutputStream fos = new FileOutputStream("test.avi"); int a; while((a = fis.read())!= -1){ fos.write(a); } fis.close(); fos.close(); } }
-
Packaged Copies [Easy memory overflow, not recommended for development]
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("20.19_day20 summary.avi"); FileOutputStream fos = new FileOutputStream("test.avi"); // int a; // while((a = fis.read())!= -1){ // fos.write(a); // } byte[] arr = new byte[fis.available()]; fis.read(arr); fos.write(arr); fis.close(); fos.close(); } }
Define a copy of the decimal array [Development Recommendation]
General Form
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("cccccc.txt"); FileOutputStream fos = new FileOutputStream("bbbbbb.txt"); byte[] arr = new byte[2]; int len; //Number of valid characters read while((len = fis.read(arr))!= -1){ fos.write(arr,0,len); } fis.close(); fos.close(); } }
standard format
package com.test.demo001; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("cccccc.txt"); FileOutputStream fos = new FileOutputStream("bbbbbb.txt"); byte[] arr = new byte[1024 * 8]; int len; //Number of valid characters read while((len = fis.read(arr))!= -1){ fos.write(arr,0,len); } fis.close(); fos.close(); } }