1

I am passing a file path to this method which writes the in txt file. But when I run this program it is not writing full and I don't know where I made mistake.

public void content(String s) {
 try { 
 BufferedReader br=new BufferedReader(new FileReader(s)); 
 try {
 String read=s;
 while((read = br.readLine()) != null) { 
 PrintWriter out = new PrintWriter(new FileWriter("e:\\OP.txt"));
 out.write(read);
 out.close();
 }
 } catch(Exception e) { } 
 } catch(Exception e) { }
}
tehlexx
2,85718 silver badges29 bronze badges
asked Jan 24, 2013 at 15:10
2
  • 5
    You shouldn't just silently catch the exception. Then you might get a meaningful error message. Commented Jan 24, 2013 at 15:11
  • Just some char is written on the output file. Not the entire content. Whats the problem? Commented Jan 24, 2013 at 15:13

5 Answers 5

7

You shouldn't create your PrintWriter inside the loop every time:

public void content(String s) {
 BufferedReader br=new BufferedReader(new FileReader(s));
 try {
 PrintWriter out=new PrintWriter(new FileWriter("e:\\OP.txt"));
 String read=null;
 while((read=br.readLine())!=null) {
 out.write(read);
 }
 } catch(Exception e) {
 //do something meaningfull}
 } finally {
 out.close();
 }
}

Aditionally, as others have mentioned add a finally block, do not silently catch the exception, and follow the Java Coding Conventions.

answered Jan 24, 2013 at 15:15
1
  • stackoverflow.com/a/14504685/2003348 This answer is answer. Nice. As your coding not formatted the OP file in proper alignment. This answer solved my problem :D Commented Jan 24, 2013 at 15:28
1

close your PrintWriter inside finally block out side the loop

 finally {
 out.close();
 }
answered Jan 24, 2013 at 15:11
1

It's better to use Apache Commons IO instead.

http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html should make the trick.

(Unless you are trying to learn the low-level stuff or actually knows why you can't use IOUtils for this case.)

answered Jan 24, 2013 at 15:18
0

try this

public void content(String s) throws IOException { 
 try (BufferedReader br = new BufferedReader(new FileReader(s));
 PrintWriter pr = new PrintWriter(new File("e:\\OP.txt"))) {
 for (String line; (line = br.readLine()) != null;) {
 pr.println(line);
 }
 }
}
answered Jan 24, 2013 at 15:18
0
0

Your closing stream before finishing it. So either put it into

<code>
finally {
out.close();
}
</code>
or see this simple example
<code>try {
 String content = s;
 File file = new File("/filename.txt");
 // if file doesnt exists, then create it
 if (!file.exists()) {
 file.createNewFile();
 }
 FileWriter fw = new FileWriter(file.getAbsoluteFile());
 BufferedWriter bw = new BufferedWriter(fw);
 bw.write(content);
 bw.close();
 System.out.println("Done");
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
</code>
answered Jan 24, 2013 at 15:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.