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());
}
}
}
程序執行成功,但是當我打開輸出文件Xanadu.txt時,什麼都看不到。有人可以指導我嗎?文件Xanadu.txt的內容是一個包含兩個屬性name和age的對象stemp數組。無法寫入文件的內容
感謝,它的工作! – 2012-02-04 18:57:49