简体   繁体   中英

System.out.println not working after printWriter on System.out

I am facing a rather unusual situation. I am using printWriter with System.out to print the bytes from a file onto console, first using a ByteStreamReader and then a CharacterStreamReader . The problem is after the output of ByteStreamReader , subsequent System.out.println statements are not printing anything on console. I tried using flush on System.out.flush() but still not getting output.

Code:

FileStream fs = new FileStream(); //ByteStreamReader
String source = "./test_zh.txt";
CharacterStream cs = new CharacterStream(); //CharacterStreamReader
System.out.println("\nChinese Sentence using ByteStreamReader");
fs.printOnConsole(source); //if i comment this, then below lines gets printed 
System.out.println("\nChinese Sentence using CharacterStreamReader");//Not getting printed on console
cs.printOnConsole(source); //Not getting printed on console

My code for both the Stream types is as follows:

FileSteam Class

public void printOnConsole(String source) throws IOException{
try{
    inp = new FileInputStream(source);
    pwr = new PrintWriter(System.out,true);
    int c =0;

    while( (c = inp.read()) != -1){
        pwr.write(c);
        pwr.write("(" + c +")");
    }
}catch(FileNotFoundException f){

}catch(IOException e){

}finally{
    inp.close();
    pwr.flush();
    pwr.close();

}
}

CharacterStream Class

    public void printOnConsole(String source) throws IOException{
    try{
        fReader = new FileReader(source);
        pwr     = new PrintWriter(System.out,true);
        int c;
        while((c = fReader.read()) != -1){
            pwr.write(c);
            pwr.write("(" +c +")");
        }

    }catch(IOException f){

    }finally{
        fReader.close();
        pwr.flush();
        pwr.close();

    }
    }

I am facing a rather unusual situation. I am using printWriter with System.out to print the bytes from a file onto console, first using a ByteStreamReader and then a CharacterStreamReader . The problem is after the output of ByteStreamReader , subsequent System.out.println statements are not printing anything on console. I tried using flush on System.out.flush() but still not getting output.

Code:

FileStream fs = new FileStream(); //ByteStreamReader
String source = "./test_zh.txt";
CharacterStream cs = new CharacterStream(); //CharacterStreamReader
System.out.println("\nChinese Sentence using ByteStreamReader");
fs.printOnConsole(source); //if i comment this, then below lines gets printed 
System.out.println("\nChinese Sentence using CharacterStreamReader");//Not getting printed on console
cs.printOnConsole(source); //Not getting printed on console

My code for both the Stream types is as follows:

FileSteam Class

public void printOnConsole(String source) throws IOException{
try{
    inp = new FileInputStream(source);
    pwr = new PrintWriter(System.out,true);
    int c =0;

    while( (c = inp.read()) != -1){
        pwr.write(c);
        pwr.write("(" + c +")");
    }
}catch(FileNotFoundException f){

}catch(IOException e){

}finally{
    inp.close();
    pwr.flush();
    pwr.close();

}
}

CharacterStream Class

    public void printOnConsole(String source) throws IOException{
    try{
        fReader = new FileReader(source);
        pwr     = new PrintWriter(System.out,true);
        int c;
        while((c = fReader.read()) != -1){
            pwr.write(c);
            pwr.write("(" +c +")");
        }

    }catch(IOException f){

    }finally{
        fReader.close();
        pwr.flush();
        pwr.close();

    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM