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 ?