One, Java read and store file data streamJava reads a file, which is actually the process of converting a byte stream in a file into a character flow output to the screen This involves two classes: InputStreamReader and OutputStreamWriterinputstreamreader: Convert byte streams to character streamOutputStreamWriter: Converting character flow to byte stream InputStreamReader inheriting reader classOutputStreamWriter inheriting writer class Second, Bufferedreader/filereader/inputstreamreaderreader is used to read the contents of a file, and writer is used to write characters to a file. Reader is an abstract class for reading stream of characters1. Bufferedreader/filereader/inputstreamreader's inheritance relationship is as follows:so by bufferedreader/filereader/inputstreamreader these three methods to read the file, you can directly output characters2. How to use the Bufferedreader/filereader/inputstreamreader and the difference
InputStreamReader: You can specify the character encoding format to pass in the InputStream objectThere are two ways to define a InputStream object. System.in or FileInputStream objectsystem.in: Read input from consoleFileInputStream: Can be instantiated by file path name Pathname
InputStreamReader inputstreamreader1=New InputStreamReader (system.in); InputStreamReader Inputstreamreader2=new inputstreamreader (new FileInputStream ("/opt/xxx")); Nputstreamreader Inputstreamreader3=new inputstreamreader ( new FileInputStream (new File ("/opt/xxx"));
filereader: Import Direct File PathnameFileReader is a subclass of InputStreamReader, cannot specify character encoding, others are basically similar to InputStreamReader
FileReader f1=New filereader ("/opt/xxx.txt"); FileReader F2=new filereader (new File ("/opt/xxx.txt"));
BufferedReader: Entry with reader object and buffer size (can not be written)read the character stream from the buffer to improve efficiency; Buffer size: Default 8192, no delivery required by defaultIt is recommended to use this class to read the file
BufferedReader buffered_filereader=New BufferedReader (new filereader (filename)); BufferedReader Buffered_inputstreamreader=new bufferedreader (new InputStreamReader ( FileInputStream));
Third, FileInputStreamA superclass of all byte input streams, used for reading bytes. as already mentioned, one of the arguments applied when instantiating a InputStreamReader object can pass file path name pathname or file object instantiation Four, read the filethe BufferedReader object uses the ReadLine () method to determine whether the string is null to determine whether it is the end of the file
String Read; while ((Read=buffered_filereader.readline ()) =null) { System.out.println (read);}
The reader object uses the Read () method to determine whether 1 is the end of the file.
int i; while ((I=filereader.read ())!=-1) { System.out.print ((char) i);}
v. Java code1, Inputsteamreader
String filename= "/opt/xxx.log"; FileInputStream FileInputStream=new fileinputstream (filename); InputStreamReader InputStreamReader=new InputStreamReader (fileinputstream); int i; while ((I=inputstreamreader.read ())!=-1) { System.out.print ((char) i);}
2, FileReader
String filename= "/opt/xxx.log"; FileReader FileReader=new filereader (filename); int i; while ((I=filereader.read ())!=-1) { System.out.print ((char) i);}
3, BufferedReader (into the ginseng FileReader)
String filename= "/opt/xxx.log"; FileReader FileReader=new filereader (filename); BufferedReader Buffered_filereader=new BufferedReader (FileReader); String Read; while ((Read=buffered_filereader.readline ()) =null) { System.out.println (read);}
4, BufferedReader (into the ginseng InputStreamReader)
String filename= "/opt/xxx.log"; FileInputStream FileInputStream=new fileinputstream (filename); InputStreamReader InputStreamReader=new InputStreamReader (FileInputStream); BufferedReader Buffered_inputstreamreader=new BufferedReader (InputStreamReader); String Read; while ((Read=buffered_inputstreamreader.readline ()) =null) { System.out.println (read);}
Java read file-bufferedreader/filereader/inputstreamreader/fileinputstream relationship and differences