Q:

Unable to write contents of file

package employee;

import employee.nidhin.staples;
import java.util.*;
import java.io.*; 

public class Employee {

public static void main(String[] args) 

{
   int j=3;
   staples[] stemp = new staples[j];
  String file_name = "d:/personal/11636470/NetBeansProjects/Employee/src/employee/Xanadu.txt";

 try

 {

 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));  

 for ( j=0;j<3;j++)
        {
            stemp[j] = new staples();

            System.out.print("Enter your name : ");
            stemp[j].setName(reader.readLine());

            System.out.println("Enter your age : "); 
            stemp[j].setAge(Integer.parseInt(reader.readLine()));

        }

 for ( j=0;j<3;j++)
        {
            System.out.println("Employee number:" + j +" name:"+stemp[j].getName()+" Age:"+stemp[j].getAge() );

        }

 reader.close(); // VERY IMPORTANT TO CLOSE 

 System.out.println("Now writing the file to Xanadu.txt "); 

  PrintWriter out = new PrintWriter(
  new FileWriter("file_name"));
  for (int i = 0; i < 3; i++) 
  {
       out.println("Value at: "+ i + " = "+ stemp[i].getName());
  }

  System.out.println("Successfully wrote to file");

  out.close();

 }
 catch(java.io.IOException ex)
 {
     System.out.println("Error is " + ex.getMessage() ); 
 }

}   
}

The program execute successfully , but when I open the outputfile Xanadu.txt , I see nothing. Can someone guide me? The contents of the file Xanadu.txt is an array of objects stemp which have two attributes name and age.

A:

PrintWriter out = new PrintWriter(new FileWriter("file_name"));

You're passing a String that says: "file_name",
try doing that:
PrintWriter out = new PrintWriter(new FileWriter(new File(file_name)));

I think it should work. As FileWriter constructor should take File as a parameter, I think.

