34.3 commutations InputStreamReader OutStreamReader

Commutations: the output byte stream into an output stream of characters

Standard input and output streams: byte stream is subjected to transmission System.in, System.out

 

Stdio stream

Final static public the InputStream in: byte input stream for reading the keyboard data entry 
public static int Final X; 
the InputStream IS = the System.in; 
Scanner Scanner new new SC = (the System.in); 
public static Final PrintStream OUT: Word section output stream, outputs the data to the command line System.out.println ();

  

Conversion flow

Import the java.io. * ; 

/ * 
 * Demand: reading keyboard input data, and outputs to the file in the project a.txt root directory 
 * 
 * Source: System.in keyboard input of data read (byte stream ) 
 * destination: a.txt FileWriter (character stream) in the project root directory 
 * 
 * 
 * 
 * conversion flow: the need to enter the byte stream into a character-input stream, InputStreamReader 
 * InputStreamReader (InputStream in) 
 * is the byte streams InputStreamReader bridge to the character stream 
 * / 
public  class InputStreamReaderDemo {
     public  static  void main (String [] args) throws IOException {
 //         Method ();
 //         method2 (); 
        the method3 (); 



    } 

    //Input byte character turn
     // output character 
    Private  static  void the method3 () throws IOException {
         // Create an object input stream 
        the InputStream IPS = the System.in; 
        the InputStreamReader IPR = new new the InputStreamReader (IPS); 

        // create output stream object 
        FileWriter FW = new new FileWriter ( "io.txt" ); 

        char [] = CHS new new  char [1024 ];
         int len;
         the while (! (len = ipr.read (CHS)) = -. 1 ) { 
            fw.write (CHS, 0 , len );
            fw.flush (); 
        } 

        fw.close (); 
        ipr.close (); 
    } 

    // input byte
     // outputs the character (string of bytes output rpm) 
    Private  static  void method2 () throws IOException { 
        the InputStream IPS = the System.in; 
        FileWriter FW = new new FileWriter ( "io.txt" ); 

        byte [] = by new new  byte [1024 ];
         int len;
         the while (! (len = ips.read (by)) = -. 1 ) { 
            FW .write ( new new String (by, 0, len)); // constructor String (byte [] bytes) String Class encapsulated into the byte data string object 
            fw.flush (); 
        } 

        fw.close (); 
        ips.close (); 
    } 

    // byte inputs
     // byte output 
    Private  static  void Method () throws IOException {
         // data source 
        the InputStream IPS = the System.in;
         // destination 
        the OutputStream fos = new new a FileOutputStream ( "io.txt" ); 

        byte [] = by new new  byte [1024 ] ;
         int len;
         the while ((len=ips.read(by))!=-1) {
            fos.write(by,0,len);
            fos.flush();
        }

        //释放资源
        ips.close();
        fos.close();
    }
}

 

 

Import the java.io. * ; 

/ * 
 * Demand: reading SystemInOutDemo.java the project root, and outputs to the command line 
 * Data Source: SystemInOutDemo.java BufferedReader the project root 
 * Destination: command System.out 
 * 
 * 
 * 
 * Since the standard output stream is a stream of output bytes, the bytes or byte array output only, but we read data string is, if you want to output further be converted into a byte array 
 * If we want to pass the standard output stream string, the standard output stream to the output stream can be one of the character, the OutputStreamWriter 
 * 
 * the OutputStreamWriter (the OutputStream OUT): commutations, the output byte stream into an output stream of characters 
 * 
 * 
 * / 
public  class OutputStreamWriterDemo {
     public  static  void main (String [] args) throws IOException {
 //         Method (); 

        //源端:文本文件
        /*FileReader fr = new FileReader("io.txt");
        BufferedReader br = new BufferedReader(fr);*/
        BufferedReader br = new BufferedReader(new FileReader("io.txt"));

        //目的地:命令行
        /*OutputStream ops = System.out;
        Writer opw = new OutputStreamWriter(ops);
        BufferedWriter bw = new BufferedWriter(opw);*/
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String line;
        while((line=br.readLine())!=null) {
            bw.write (Line); 
            bw.newLine (); 
        } 

        // release resources 
        br.close (); 
        bw.close (); 



    } 

    // read character
     // output character byte transfer (byte the System.out flow) 
    Private  static  void Method () throws IOException { 
        the BufferedReader br = new new the BufferedReader ( new new the FileReader ( "io.txt" )); 
        the OutputStream OS = the System.out; 

        / * after reading the last line of the next row to return null 
        the System.out .println (br.readLine ());  
        System.out.println (br.readLine ());
        System.out.println (br.readLine ()); * /
        String line;
        while ((line=br.readLine())!=null) {
            os.write(line.getBytes());
            os.write("\r\n".getBytes());
            os.flush();
        }

        os.close();
        br.close();
    }
}

 

Guess you like

Origin www.cnblogs.com/longesang/p/11320089.html