• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Reading Terminal Input

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i enter 5 lines of input by pressing enter, it writes only the 1st line to the file. why is that?

import java.io.*;
class Editor {
public static void main(String[] args) throws IOException{
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("Test.txt");
PrintWriter pw = new PrintWriter(fw, false);
String s = "p";
while(true){
try{
s = bin.readLine();
System.out.println(s);
pw.println(s);
if (s.equals(new String("done"))) {
System.out.println(s);
break;
}
}
catch(IOException e) {
System.out.println("Error " + e);
}
pw.close();
}
}
}
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that's because you close your PrintWriter after the first pass, so nothing is written after that. After moving pw.close(): all input is written to the file.
Regards,
Anna
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"super co"
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
 
Ranjani Swa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it worked! I didnt realise that it was inside the loop
reply
    Bookmark Topic Watch Topic
  • New Topic