Commutations standard input and output streams

Conversion role stream

 

TestOtherStream

Package com.aff.file; 

Import java.io.BufferedReader;
 Import java.io.BufferedWriter;
 Import java.io.File;
 Import a java.io.FileInputStream;
 Import java.io.FileOutputStream;
 Import java.io.IOException;
 Import a java.io.InputStream;
 Import the java.io.InputStreamReader;
 Import java.io.OutputStreamWriter;
 Import org.junit.Test; 

// commutations the OutputStreamWriter the InputStreamReader
 //     Code: string ---> byte array
 @     decoding: byte array ---> string 

public  class TestOtherStream { 

    @Test
    public void test1() {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            // 解码
            File file1 = new File("jdbc.properties");
            FileInputStream fis = new FileInputStream(file1);
            InputStreamReader isr = new InputStreamReader(fis, "utf-8");
            br = new BufferedReader(isr);

            // 编码
            File file2 = new File("jdbc6.properties");
            FileOutputStream fos = new FileOutputStream(file2);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            bw = new BufferedWriter(osw);

            String str;
            while ((str = br.readLine()) != null) {
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
             IF (BW =! Null ) {
                 the try { 
                    bw.close (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
            IF (! Br = null ) {
                 the try { 
                    br.close (); 
                } the catch (IOException E) { 
                    e.printStackTrace (); 
                } 
            } 
        } 
    } 


// standard input and output streams // Exercise: the input from the keyboard character stream, requested to read the entire string of uppercase transfer output, // then proceed with the input operation, when the input e or until the exit, exit the program @Test public void test2 () { the BufferedReader br = null ; / * * standard input and output streams: standard input stream: * the System.in standard output stream: the System.out * / the try { the InputStream iS = the System.in; // byte stream the InputStreamReader ISR = new new the InputStreamReader (iS); // byte stream into a character stream br = new new the BufferedReader (ISR); // packet buffer layer outside the character stream flow String str; while (true) { System.out.println("输入字符串"); str = br.readLine(); if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) { break; } String str1 = str.toUpperCase(); System.out.println(str1); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

 

Guess you like

Origin www.cnblogs.com/afangfang/p/12608860.html