java-based - the standard input and output redirection, data stream

These two streams feel very practical

The first is the re system.in/out

/ * 
 * 1. The standard input and output streams 
 * System.in: standard input stream, input from the keyboard default 
 * the System.out standard output stream, the default output from the console 
 setIn * System class (InputStream is) / setOut (PrintStream ps) mode reassign 
 *      
 * * / 

public  class OtherStreamTest {
     / * 
     * method a: Scanner implemented using, call next () method may be 
     * two: System.in implemented read, System.in -> commutations - > BufferedReader of the readline () 
     *      
     * * / 
    @Test 
    public  void test1 () {
         // get the standard input stream 
        the BufferedReader br = null ;
         the try {
             //System.in stream is a stream of bytes, the character stream to be converted into 
            the InputStreamReader ISR = new new the InputStreamReader (the System. In );
             // then the character stream buffer transfer packing 
            br = new new the BufferedReader (ISR); 
            
            the while ( to true ) { 
                Data String = br.readLine (); // read directly into a line 
                IF ( " E " .equalsIgnoreCase (Data) || " Exit " .equalsIgnoreCase (Data)) { 
                    the System. OUT .println ( " end of program " );
                     BREAK;
                }
                String upperString = data.toUpperCase();
                System.out.println(upperString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(br!=null)
                    br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /*打印流
     * Output stream of bytes PrintStream
     * Character output stream the PrintWriter 
     * overloaded provide a series of print () and the println () 
     * * / 
    @Test 
    public  void test2 () throws a FileNotFoundException {
         // file output stream hello.txt binding 
        PrintStream PS = null ;
         the try {
             / / redirect the standard output step
             // 1.New byte stream objects 
            a FileOutputStream fos = new a FileOutputStream ( new (File " hello.txt " ));
             // 2. a new byte stream to the output stream of bytes ps, this print stream to stream 
            PS =new new  PrintStream (fos,to true );
             // 3. cmd from the standard output stream redirected to print PS 
            IF (PS =! null ) 
                System.setOut (PS); 
            
            
            
            for ( int I = 0 ; I < 255 ; I ++ ) { 
                the System. OUT .print (( char ) I);
                 IF (I% 50 == 0 ) 
                    the System. OUT .println (); 
            } 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        the finally {
            try {
                if(ps!=null)
                    ps.close();
            } catch (Exception e) { 
                e.printStackTrace();
            }
        }
    }
    

The second stream is used to read the stream from the file / write data such as Int

/ * 
     * Data stream: DataInputStream: sets the InputStream and OutputStream above 
     * Function: Basic for reading or writing data types of variables or string 
     *          
     * * / 
    @Test 
    public  void Test3 () { 
        the DataOutputStream DOS = null ;
         the try {
             // string in memory, the basic data types of variables written to a file 
            DOS = new new the DataOutputStream ( new new a FileOutputStream ( new new (file " hello.txt " ))); 
            dos.writeUTF ( " zsben " ); 
            DOS. flush (); 
            DOS. writeInt ( 23);
            dos.writeBoolean(true);
            dos.flush();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                if(dos!=null)
                    dos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/zsben991126/p/12161118.html