繁体   English   中英

打印机不写入文件

[英]Printwriter not writing to file

output 文件已创建,但未写入数字。

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 ");
                    }


                }
            }
    }
}

程序应该读取一个文件,然后创建一个 output 文件,该文件反向打印输入文件中的数字。

实际结果只显示文件,但文件中没有写入任何内容。

For 循环条件错误,因此循环中的代码不会运行。

我想应该是

                      for (int i = 99; i >= 0; i--)
                    {
                        output.print(hold_ints[i]);
                        output.print(" ");
                    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM