2019-05-31 Java learning diary day21 IO stream character stream

I

Jifuryu

  Character stream can be directly read and write IO streams characters

  Character stream reads characters, we must first read byte data, and then converted to a character, if the character to write, you need to re-write the characters into bytes

FileReader

  read FileReader class () method reads a character size according to the

public  class demo1_filereader { 

    public  static  void main (String [] args) throws IOException {
         // the demo1 (); 
        the FileReader F1 = new new the FileReader ( "xxx.txt" );
         int X;
         the while ((X = f1.read ()) ! = -1) {   // read a character code table by a default item 
            of System.out.print (( char ) X); 
        } 
    } 

    public  static  void the demo1 () throws a FileNotFoundException, IOException { 
        the FileReader F1 = new new FileReader("xxx.txt");
        int x=f1.read();
        System.out.println(x);
        char c=(char)x;
        System.out.println(c);
        f1.close();
    }

}
Case

 

write () method of class FileWriter automatically write the character to the byte

public static void main(String[] args) throws IOException {
        FileWriter f1=new FileWriter("tan.txt");
        f1.write("大家好");
        
        f1.close();

    }

}
Case

 

Copy a character stream

public  class demo3_fierreader { 

    public  static  void main (String [] args) throws IOException { 
        the FileReader F1 = new new the FileReader ( "xxx.txt" ); 
        FileWriter F2 = new new FileWriter ( "zzz.txt" ); 
        
        int C;
         the while ((C ! = f1.read ()) = -1 ) { 
            f2.write (C); 
        } 
        f1.close (); 
        f2.close ();   // Writer class has a small buffer 2k, and if none of stream will write the contents of the buffer, the flow will be shut, the buffer will be refreshed within the buffer 
        

    } 

}
Case

 

Under what circumstances a character stream

  * Character stream can also copy the text file, but not recommended, because the characters will Byte to read, write Shihai put back byte characters

  * The program needs to read a text, or need to write a piece of text can be used when a character stream

    Read in accordance with the size of the characters, half Chinese will not appear when the time is read

    When writing directly write a string, instead of converting an array of bytes

Whether the character stream can copy non-plain-text files

  You can not copy the text of the non-plain text

  When read as bytes will be converted to characters in the conversion process, the corresponding characters may not be found, it will use? Instead of, when converting the string is written into a byte write out

  If the time? Direct write, so write the file after the chaos, you can not see the

 

Custom copy of the array of characters

                F1 = the FileReader new new the FileReader ( "xxx.txt" ); 
        FileWriter F2 = new new FileWriter ( "yyy.txt" );
         char [] = ARR new new  char [1024 ];
         int len; 
         the while ((len = f1.read (ARR !)) = -1) { // read data to the file on the array of characters 
            f2.write (ARR, 0, len);             // data character array written document 
        } 
        f1.close () ; 
        f2.close ();        

 

Buffered character stream

BufferedReader's read () method will first read when reading characters into a buffer of several characters, one by one and then returns to the program, reduce the number of read documents, improve efficiency

BufferdWriter the write () method will first write buffer is written character, will be written to the file when the buffer is full, reduce the number of written documents, improve efficiency

 

the readLine () and newLine () method

BufferedReader of the readLine () method reads a line of characters (no line breaks)

public class demo4_buffered {

    public static void main(String[] args) throws IOException {
        BufferedReader b1 =new BufferedReader(new FileReader("xxx.txt"));
        String line;
        
        while((line = b1.readLine()) != null){
            System.out.println(line);
        }
        b1.close();
    }

}
Case

BufferdWriter of newLine () can output a cross-platform newline "\ r \ n"

newLine () method is cross-platform

"\ R \ n" is only supported by windows system

BufferedReader b1 =new BufferedReader(new FileReader("xxx.txt"));
        BufferedWriter b2=new BufferedWriter(new FileWriter("yyy.txt"));
        String line;
        while((line= b1.readLine()) !=null){
            b2.write(line);
            b2.newLine();
        }
        b1.close();
        b2.close();
Case

 

The reverse text

public  class test1 { 

    public  static  void main (String [] args) throws IOException {
         // Create the input and output stream objects 
        the BufferedReader br = new new the BufferedReader ( new new the FileReader ( "xxx.txt" )); 
        
        // create the collection object 
        ArrayList <String> = List new new the ArrayList <> ();
         // the read data is stored in the collection 
        String Line;
         the while (! (= br.readLine Line ()) = null ) { 
            List.add (Line); 
        } 
        br.close (); // off stream
         //Jiang Shuji written backwards through the collection file 
        BufferedWriter, BW = new new BufferedWriter, ( new new FileWriter ( "jung.txt" ));
         for ( int I = list.size () -1; I> = 0; i-- ) { 
            bw.write (List.get (I)); 
            bw.newLine (); 
        } 
        

        bw.close (); 
    } 

}
As early as late opening off

 

LineNumberReader

  When a BufferedReader LineNumberReader subclasses have the same functionality, and can count the number of rows

    * Call getLineNumder () method can get the current line number

    * Call getLineNumder () method to set the current line number

public class demo5_number {
    public static void main (String args []) throws IOException{
        LineNumberReader num1=new LineNumberReader(new FileReader("jung.txt"));
         String line ;
         num1.setLineNumber(100);
         while((line =num1.readLine()) !=null){
             System.out.println(num1.getLineNumber()+":"+line);
         }
         num1.close();
    }
}
Case

 

Guess you like

Origin www.cnblogs.com/JungTan0113/p/10958276.html