【发布时间】:2019-10-22 01:00:05
【问题描述】:
创建了输出文件,但没有写入数字。
public static void main(String[] args)
{
String file_name;
String done_string = "done";
boolean done = false;
while (!done)
{
file_name = JOptionPane.showInputDialog("Enter a file name" + " or done to exit: ");
if (file_name.equals(done_string))
{
done = true;
JOptionPane.showMessageDialog(null, "EXITING");
}
else
{
try
{
File file_in = new File(file_name);
Scanner input = new Scanner(file_in);
JOptionPane.showMessageDialog(null, "File " + file_name + " found ");
int[] hold_ints = new int[100];
for (int i = 0; i< 100; i++)
{
hold_ints[i] = input.nextInt();
}
PrintWriter output = new PrintWriter("reverse_ints");
for (int i = 99; i <= 0; i--)
{
output.print(hold_ints[i]);
output.print(" ");
}
output.close();
input.close();
}
catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "File " + file_name + " not found ");
}
}
}
}
}
程序应该读取一个文件,然后创建一个输出文件,以反向打印输入文件中的数字。
实际结果只显示文件,但文件中没有写入任何内容。
【问题讨论】:
-
请在 printwriter 构造函数中提供文件名或流。google.com/amp/s/www.geeksforgeeks.org/…
标签: java printwriter