Role: IO stream processing data transmission between devices, data input and output are in the form of streams (stream)
1), stream classification:
> Distinguish by data unit: byte stream (8bit), character stream (16bit)>Different by flow direction: input stream, output stream
Different by role: Node Streams (FileInputStream, FileOutputStream, FileReader, FileWriter), Processing Streams
2), input stream:
InputStream (byte stream), Reader (character stream)output stream:
OutputStream (byte stream), Writer (character stream)3) Java's IO streams are all derived from four abstract base classes, and the names of subclasses derived from these four classes are all suffixed with the name of the parent class.
①, four abstract base classes: InputStream, OutputStream, Reader, Writer②, access files: FileInputStream, FileReader
③, access array: ByteArrayInputStream, CharArrayReader
④, Buffered Stream: BufferedInputStream, BufferedReader
4), input stream: read the file, such as:
(1). To create a new File object, the file to be read must exist:File file = new File("Hello.txt");
(2). Create an input stream object
FileInputStream fis = null;
fis = new FileInputStream(file) ;
(3). Call the method of FileInputStream to read data
//method 1: read only one byte at a time
int len;
while((len = fis.read()) != -1){
System.out.print( (char)len);
}
//Displayed manual close stream
if(fis != null){ //Avoid null pointer
fis.close(); //Avoid memory leak
}
//Method 2: use byte array storage, For example:
int len; //Record the number of bytes read into the byte array each time
byte[] bs = new byte[1024]; //The length of the byte array can be customized
while((len = fis.read ()) != -1){
// Traversal method 1: bs.length cannot be replaced by len.
for(int i = 0; i < len; i++){
System.out.print((char)bs[i]);
}
//traversal method 2: use String, bs represents the byte array for storing data, 0 represents The starting position of reading, len represents the length of reading, cannot use bs.length
String str = new String(bs, 0, len);
System.out.println(str);
}
if(fis != null){
fis.close();
}
5), output stream: write files to disk, such as:
(1). Create a file object that needs to be written, which can be a parameter containing a directoryFile file = new File("My.txt");或者 File file = new File("D:/My.txt");
(2). Create an output stream object
FileOutputStream fos = null;
fos = new FileOutputStream("file");
(3). Create a byte array
String str = new String("I Love You!");
//Convert string to byte array by getBytes() method
byte[] myByte = str.getBytes();
(4). Write data by calling the FileOutputStream method
fos.write(myByte);
(*): For the output stream, if the file does not exist, the file will be created automatically; if it exists, the data in the file will be overwritten. When writing, the data can be in Chinese , such as :
String myInfo = new String("Silly girl, I like you!");
byte[] info = myInfo.getBytes();
(5). Close the stream, and close the output stream displayed to avoid memory leaks
if(fos != null){
fos.close();
}
6), copy the file:
(1). Create a new file object
File inFile = new File("Hello.txt");
File outFile = new File("Myfile.txt");
(2). Create input and output stream objects
FileInputStream fis = null;
FileOutputStream fos = null;
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
(3). Read data from the input stream and write to the output stream
byte[] bs = new byte[1024];
int len;
while((len = fis.read()) != -1){
fos.write(bs, 0, len);//Note that fos.write(bs) cannot be used, it has the same effect as fos.write(bs, 0, bs.length)
}
(4). Remember: the closed current that must be displayed
if(fis != null){
fis.close();
}else if(fos != null){
fos.close();
}
Code example:
Copy code example of file: