Java File Reading and java reading
Read can be read in bytes, in characters, or a whole line.
FileInputStream is read by byte.Read one byte at a timeIt is often used to read binary file slices, sounds, images, and so on.
1 try { 2 aFileInputStream=new FileInputStream(aFile); 3 // FileInputStream aFileInputStream2=new FileInputStream("E:/a.txt"); 4 int ch; 5 while((ch=aFileInputStream.read())!=-1) 6 { 7 System.out.print((char)ch); 8 } 9 } catch (FileNotFoundException e) {10 // TODO Auto-generated catch block11 e.printStackTrace();12 } catch (IOException e) {13 // TODO Auto-generated catch block14 e.printStackTrace();15 }finally {16 if(aFileInputStream!=null)17 {18 try {19 aFileInputStream.close();20 } catch (IOException e) {21 // TODO Auto-generated catch block22 e.printStackTrace();23 } 24 }25 }
We recommend that you first create a file reference and then use exists () to check whether the file exists. This is faster than the annotated code to directly throw an exception, when using this function, you must close the file input stream.
FileReader or InputStreamReader is used to read the bytes of a single character at a time in characters. It is often used to read text and digital files.
1 try { 2 FileReader fileReader=new FileReader(aFile); 3 // FileReader aFileReader=new FileReader("E:/a.txt"); 4 int a; 5 while((a=fileReader.read())!=-1) 6 { 7 System.out.print((char)a); 8 } 9 10 fileReader.close();11 } catch (FileNotFoundException e) {12 // TODO Auto-generated catch block13 e.printStackTrace();14 } catch (IOException e) {15 // TODO Auto-generated catch block16 e.printStackTrace();17 }
However, in windows, \ r \ n represents a line break when they are both in combination. However, if the two characters are displayed separately, the line is changed twice. Therefore, \ r or \ n must be blocked. Otherwise, there will be a lot more blank lines. You can also directly read multiple characters.
1 try {2 FileReader fileReader = new FileReader (aFile); 3 4 char [] a = new char [20]; 5 while (fileReader. ready () 6 {7 fileReader. read (a); // The returned read length is 8} 9 System. out. println (String. valueOf (a); 10 fileReader. close (); 11} catch (FileNotFoundException e) {12 // TODO Auto-generated catch block13 e. printStackTrace (); 14} catch (IOException e) {15 // TODO Auto-generated catch block16 e. printStackTrace (); 17}
It is best to put the closed input stream in finally to avoid the case that the stream is not closed after an exception is thrown.
At last, BufferedReader can be read as a row and is often used to read row-oriented formatted files.
1 BufferedReader bufferedReader=null; 2 try { 3 bufferedReader=new BufferedReader(new FileReader(aFile)); 4 String aString; 5 while((aString=bufferedReader.readLine())!=null) 6 { 7 System.out.println(aString); 8 } 9 10 } catch (FileNotFoundException e) {11 // TODO Auto-generated catch block12 e.printStackTrace();13 } catch (IOException e) {14 // TODO Auto-generated catch block15 e.printStackTrace();16 }finally {17 if (bufferedReader!=null) {18 try {19 bufferedReader.close();20 } catch (IOException e) {21 // TODO Auto-generated catch block22 e.printStackTrace();23 }24 }25 }