-1
String text;
try {
    PrintStream pw2 = new PrintStream(new FileOutputStream("C:\\Users\\jadit\\Desktop\\ts.doc"));
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    text = br.readLine();          //Reading String  

    System.out.println(text);
    pw2.print(text);
    pw2.close();
    isr.close();
    br.close();
    }
catch(Exception e) {
    System.out.println(e);
}

int str;

try {     
    FileInputStream fr2 = new FileInputStream("C:\\Users\\jadit\\Desktop\\ts.doc"); 
    BufferedInputStream br2 = new BufferedInputStream(fr2);
    PrintStream pw1 = new PrintStream(System.out, true);

    while ((str=br2.read()) >= 0)
        pw1.println("   "+str);

    fr2.close();
    pw1.close();
    br2.close();
}
catch(Exception e){}

output:

run:
a b c d 
a b c d 
   97
   32
   98
   32
   99
   32
   100
   32

If I am trying to read the contents of some other file say, t.txt in the second try block then it is not executing or reading the contents of file t.txt, but when I am reading the contents of the same file that is being written in first try block it is displaying the contents as shown above in the output. So even though the streams are being closed in first try block itself and are being opened in the next try block, why is this happening? Can't we work differently on different files in the same program ?

Sergey
  • 176
  • 2
  • 12
  • 1
    Your question is not clear to me, and also I can't correlate the output with your actual Java code. – Tim Biegeleisen May 04 '17 at 05:59
  • 1
    Going to add on @TimBiegeleisen that i also don´t really understand what you do want, but `reading the content`, What is the content? You´re reading them as `int` and are doing an empty catch here. Maybe you´re running into an `Exception` that you do never notice as you´re exception handling doesn´t do anything? (I´m pretty certain you do) – SomeJavaGuy May 04 '17 at 06:00
  • @TimBiegeleisen - Actually, i have run this code on netbeans and this is the output i am getting. actually i had this question in which i am supposed to ' write some content in a file 'a' using standard input and print the contents of some another file 'b' which is already created'. Now the first part of question is happening to be fine but in the second part if i am trying to read file 'b's' contents , that is not happening , whereas on contrary if am reading file 'a's contents it is printing/executing. So i am stuck. – GARIMA JAIN May 04 '17 at 06:11
  • @Nathan Hughes- can you help out ?? – GARIMA JAIN May 04 '17 at 06:55
  • @SomeJavaGuy-Actually, i have run this code on netbeans and this is the output i am getting. actually i had this question in which i am supposed to ' write some content in a file 'a' using standard input and print the contents of some another file 'b' which is already created'. Now the first part of question is happening to be fine but in the second part if i am trying to read file 'b's' contents , that is not happening , whereas on contrary if am reading file 'a's contents it is printing/executing. So i am stuck. – GARIMA JAIN May 04 '17 at 06:56
  • I executed this code and I'm not getting errors in both scenarios, so it's hard to tell what is wrong without more information. Change `catch(Exception e){}` to `catch(Exception e){e.printStackTrace();}`, and then post the exception here. Or if there is nothing happening, post the contents of the files before and after execution. – tima May 04 '17 at 07:01

2 Answers2

0

If my understanding of your requirement is right, you are

  1. Trying to read a content from Standard Input and write it to file.
  2. Trying to read a content from a file and write it to standard output.

Reading a content from standard input and writing it to a file works but you are having trouble reading content from a file and writing it to a standard output.

The following code will help you achieve the second part.

    try 
    {
        FileReader fr = new FileReader("C:\\Users\\jadit\\Desktop\\ts.doc");
        BufferedReader br = new BufferedReader(fr);

        String str = null;
        while ((str = br.readLine()) != null)
        {
            System.out.println(str);
        }

        fr.close();
        br.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
anthoon
  • 101
  • 9
  • @anthoon-thank you so much. :)it works perfectly fine :) – GARIMA JAIN May 04 '17 at 07:03
  • @GARIMAJAIN Glad I could help. Please read https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/ and https://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ to understand things better. – anthoon May 04 '17 at 07:20
0

Well, your second try catch block was printing ascii values of the text in file because you are printing 'str' without converting it to character

What you have to do is replace pw1.println(" "+str); with this:

char c = (char)str;

pw1.println(" "+c);

and it shall give you content of file instead of their ascii values.

Ankur
  • 892
  • 6
  • 11