【发布时间】:2013-05-11 23:53:45
【问题描述】:
我们一直在运行此错误。目标是将数组中的数据读入输出文件。感谢您的帮助!
public static void save(Salesperson[] array)
{
PrintStream outfile = null;
try
{
outfile = new PrintStream(new FileOutputStream("data.txt"));
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"The file could not be created.");
}
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
for(int k = 0; k < array.length; k++)
{
System.out.println(array[k]);
}
outfile.close();
System.out.println("Saved.");
}
【问题讨论】:
-
好吧,那你有什么不明白的?这对我来说似乎很清楚。你对受检异常了解多少?你真的需要重定向
System.out吗? (为什么不直接写入文件而不进行重定向?) -
阅读 Java tutorial about exceptions 或您的 Java 入门书籍。
标签: java filenotfoundexception