java text reading

public  class FileReaderDemo {
     public  static  void main (String [] args) throws IOException {
         // Create a file reading stream object and associate it with the file of the specified name 
        FileReader fr = new FileReader ("demo.txt" );
         int ch = 0 ;
         while ((ch = fr.read ())! =-1 ) { 
            System.out.println (( char ) ch); 
        } 
        // Second way: read 
        System.out.println ( through character array "-------" ); 
        SecondReader (); 
    } 

    public  static  void SecondReader ()throws IOException { 
        FileReader fr = new FileReader ("demo.txt" );
         // Define a character array to store the characters read
         // The reader (char []) returns the number of characters read. 
        char [] buf = new  char [1024]; // 2k 
        int num = 0 ;
         while ((num = fr.read (buf))! =-1 ) 
            System.out.print ( new String (buf, 0 , num )); 
        fr.close (); 
    } 

}

 

Guess you like

Origin www.cnblogs.com/hongxiao2020/p/12674968.html