Regarding PrintWriter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anand Murali
    New Member
    • Oct 2010
    • 1

    Regarding PrintWriter

    My coding is as follows:

    Code:
    import java.io.*;
    class Sample
    {
    	public static void main(String args[])
    	{
    		try
    		{
    			int[] a = new int[20];
    			PrintWriter out = new PrintWriter("EG.txt");
    			System.out.println(a.length);
    			out.println(a.length);
    		}
    		catch(FileNotFoundException e)
    		{
    			
    		}
    	}
    }
    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?
    Last edited by Atli; Oct 23 '10, 10:58 AM. Reason: Please use [code] tags when posting code.
  • Will Huang

    #2
    You need to modify the PrintWriter constructor, PrintWriter constructor needs and OutputStream object, not the String to the text file.

    you can do:
    Code:
    PrintWriter out = new PrintWriter(new FileOutputStream("EG.txt"));

    Comment

    Working...