469,579 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Regarding PrintWriter

My coding is as follows:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. class Sample
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         try
  7.         {
  8.             int[] a = new int[20];
  9.             PrintWriter out = new PrintWriter("EG.txt");
  10.             System.out.println(a.length);
  11.             out.println(a.length);
  12.         }
  13.         catch(FileNotFoundException e)
  14.         {
  15.  
  16.         }
  17.     }
  18. }
After compilation and execution, in the Console i get 20 but in the file 'EG.txt' i get nothing. It is empty. What went wrong?
Oct 23 '10 #1
1 1452
You need to modify the PrintWriter constructor, PrintWriter constructor needs and OutputStream object, not the String to the text file.

you can do:
Expand|Select|Wrap|Line Numbers
  1. PrintWriter out = new PrintWriter(new FileOutputStream("EG.txt"));
  2.  
Oct 27 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Dr.Kadzija | last post: by
11 posts views Thread by Jeff Schwab | last post: by
4 posts views Thread by Francis Lavoie | last post: by
3 posts views Thread by praba kar | last post: by
3 posts views Thread by Samuel | last post: by
2 posts views Thread by Dean R. Henderson | last post: by
reply views Thread by sajithamol | last post: by
1 post views Thread by wnaveenkumar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.