1. Classification of flow
1) Classification by direction: input stream output stream
2) Classification by operation unit: byte stream character stream
3) Combination: byte input stream, byte output stream, character input stream and character output stream
2. Byte input stream
1) Abstract parent: InputStream -- cannot be instantiated
2) Common children:
1> FileInputStream -- byte input stream of operation file
Construction method parameters: file / String pathname
2> Bufferedinputstream -- efficient byte input stream
Construction method parameter: InputStream, but the abstract parent object cannot be created, so FileInputStream is passed
Case:
package cn.tedu.file;
import java.io.*;//*Indicates general distribution
/*This class is used to practice the byte input stream InputStream*/
public class TestIn {
public static void main(String[] args) {
//method();// Byte stream read
method2();//Efficient byte stream reading
}
//This method is used to test the reading of efficient byte stream
private static void method2() {
//1. Create flow object
//InputStream in = new BufferedInputStream(new FileInputStream(new File("E:\ready\1.txt")));// Not commonly used
//Create a local variable that takes effect in this method. Pay attention to manual initialization. The default value of reference type is null
InputStream in2 = null;
try {
in2 = new BufferedInputStream(new FileInputStream("E:\\ready\\1.txt"));
//2. Using flow objects
//Mode 1
// System.out.println(in2.read());
// System.out.println(in2.read());
// System.out.println(in2.read());
// System.out.println(in2.read());//-1,
//Mode II
// int b;// Define a variable to save the data read this time
// while((b=in2.read())!=-1) {/ / read the data. As long as it is not equal to - 1, it indicates that there is still data. If it meets the cycle conditions, continue the cycle
// System.out.println(b); / / print the data read in this round
// }
//Mode III
for (int i = 0; i < (new File("E:\\ready\\1.txt").length()); i++) {
System.out.println(in2.read());
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//3. Close the flow object
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//This method is used to test the reading of byte stream
private static void method() {
//Create a local variable that takes effect in this method. Pay attention to manual initialization. The default value of reference type is null
InputStream in2 = null;
try {
//1. Create a stream object. Note that InputStream is an abstract parent class and cannot be instantiated
//File file = new File("D:\\ready\\1.txt");
//String path = "D:\\ready\\1.txt";
//Alt+shift + up, move the line of code up
//InputStream in = new FileInputStream(new File("D:\ready\1.txt"); / / not commonly used
in2 = new FileInputStream("E:\\ready\\1.txt");
//2.1 use the stream object to read the data in the specified file
/*read()Each call will read a byte. If the end of the file data is read, it will return - 1,
* The return value type of this method is int, so it will find the code corresponding to the specified character and print it*/
// System.out.println(in2.read());
// System.out.println(in2.read());
// System.out.println(in2.read());
// System.out.println(in2.read());
// System.out.println(in2.read());
//2.2 optimize the code and use the loop to read the file
//Define a variable to save the data read this time
int b;
//Read the data. As long as it is not equal to - 1, it indicates that there is still data. If it meets the cycle conditions, continue the cycle
while((b = in2.read()) != -1){
System.out.println(b);//Print the data read in this round
}
} catch (Exception e) {
e.printStackTrace();//Default writing method, print error message
}finally {
/*finally{}The code block is the third part of the try catch structure
* This part will be executed when exceptions are caught, so it is often used to close the flow*/
//3. Release resources, which must be released when the flow support runs out
try {
/*The closure flow is sequential. If there are multiple flows, the last created flow will be closed first,
Multiple stream related statements require a try catch*/
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3. Byte output stream
1) Abstract parent: OutputStream -- cannot be instantiated
2) Common children:
1> Fileoutputstream -- byte output stream of operation file
Construction method parameters: file / String pathname
Note: there is a parameter boolean append by default, and the default value is false, that is, overwrite output
If the second nibbling append of the FileOutputStream constructor is set to true, the effect of additional output will be achieved
2> Bufferedoutputstream -- efficient byte output stream
Construction method parameter: OutputStream, but the abstract parent object cannot be created, so it is FileOutputStream
Case:
package cn.tedu.file;
import java.io.*;
/*This class is used to practice byte output stream*/
public class TestOut {
public static void main(String[] args) {
//method1(); / / used to test normal byte output stream
method2();//Used to test efficient byte output stream
}
//This method is used to test the efficient byte output stream BufferedOutputStream
private static void method2() {
BufferedOutputStream out = null;
//1. Create efficient byte output stream object
try {
//out = new BufferedOutputStream(new FileOutputStream(new File("E:\ready\2.txt")); / / overwrite output
// out = new BufferedOutputStream(new FileOutputStream("E:\ready\2.txt"); / / overwrite output
//out = new BufferedOutputStream(new FileOutputStream(new File("E:\ready\2.txt"),true)); / / append output
out = new BufferedOutputStream(new FileOutputStream("E:\\ready\\2.txt",true));
out.write(97);
out.write(98);
out.write(99);
out.write(100);
out.write(101);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//This method is used to test the ordinary byte output stream FileOutputStream
private static void method1() {
FileOutputStream out = null;
try {
//1. Create a normal output stream
//out = new FileOutputStream(new File("E:\ready\1.txt"); / / overwrite output
out = new FileOutputStream("E:\\ready\\1.txt");//Overlay output
//out = new FileOutputStream(new File("E:\ready\1.txt"),true); / / append output
//out = new FileOutputStream("E:\ready\1.txt",true); / / append output
//2. Using flow objects
out.write(97);
out.write(98);
out.write(99);
} catch (Exception e) {
e.printStackTrace();
}finally {//If you want the code to be executed, you need to write the data to finally
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4. Character input stream
1) Abstract parent: Reader -- can be instantiated
2) Common children:
1> FileReader -- character input stream of operation file
Construction method parameters: file / String filename
2> BufferedReader -- efficient character input stream
Constructor parameter: Reader, but cannot create abstract parent object, so FileReader is used
Case:
package cn.tedu.file;
import java.io.*;
/*This class is used to test the read operation of the character input stream Reader*/
public class TestIn2 {
public static void main(String[] args) {
//method1(); / / used to test the normal character input stream
method2();//For testing efficient character input streams
}
//This method is used to test the reading operation of efficient character input stream
private static void method2() {
BufferedReader in2 = null;
try {
//1. Create an efficient character input stream object BufferedReader
//BufferedReader in = new BufferedReader(new FileReader(new File("E:\\ready\\1.txt")));
in2 = new BufferedReader(new FileReader("E:\\ready\\1.txt"));
//2. Using flow objects
int b;
while((b = in2.read()) != -1){
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//3. Shut off
try {
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//This method is used to test the reading operation of ordinary character input stream
private static void method1() {
FileReader in = null;//Create a local variable that takes effect in this method. Pay attention to manual initialization. The default value of reference type is null
try {
//1. Create flow object
//FileReader in2 = new FileReader(new File("E:\ready\1.txt"); / / not commonly used
in = new FileReader("E:\\ready\\1.txt");
//2. Using flow objects
int b;//Define a variable to save the data read this time
while((b=in.read()) != -1){//Read the data and continue the cycle as long as it is not equal to - 1
System.out.println(b);//Print the data read in this round
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//3. Shut off
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5. Character output stream
1) Abstract parent: Writer -- can be instantiated
2) Common children:
1> Filewriter -- character output stream of operation file
Construction method parameters: file / String filename
Note: there is a parameter boolean append by default, and the default value is false, that is, overwrite output
If the second nibbling append of the FileWriter constructor is set to true, the effect of appending output will be realized
2> Bufferedwriter -- efficient character output stream
Constructor parameter: Writer, but the abstract parent object cannot be created, so FileWriter is passed
Case:
package cn.tedu.file;
import java.io.*;
/*This class is used for character output stream Writer*/
public class TestOut2 {
public static void main(String[] args) {
//method1(); / / used to test the normal character output stream
method2();//Used to test efficient character output stream
}
//This method is used to test the BufferedWriter, an efficient character output stream
private static void method2() {
BufferedWriter out = null;
try {
//1.1 create normal output stream -- overwrite output
// out = new BufferedWriter(new FileWriter(new File("E:\\ready\\2.txt")));
// out = new BufferedWriter(new FileWriter("E:\\ready\\2.txt"));
// //1.2 create efficient output stream -- efficient output
// out = new BufferedWriter(new FileWriter(new File("E:\\ready\\2.txt"),true));
out = new BufferedWriter(new FileWriter("E:\\ready\\2.txt",true));
for (int i = 0; i <100 ; i++) {
for (int j = 64; j < 90; j++) {
out.write(j + 1);
out.write(32);//Space
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//This method is used to test the ordinary character output stream FileWriter
private static void method1() {
FileWriter out = null;
try {
//1.1 create normal output stream -- overwrite output
//out = new FileWriter(new File("E:\\ready\\2.txt"));
//out = new FileWriter("E:\\ready\\2.txt");
//1.2 create normal output stream -- append output
//out = new FileWriter(new File("E:\\ready\\2.txt"),true);
out = new FileWriter("E:\\ready\\2.txt", true);
for (int i = 96; i < 127; i++) {
out.write(i + 1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}