简体   繁体   中英

Printwriter not writing to file

output file is created but numbers are not written.

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


                }
            }
    }
}

Program should read a file then create an output file that prints the numbers in the input file in reverse.

Actual results just shows the file but nothing is written in the file.

For-loop condition is wrong, so code in the loop is not run.

I suppose it should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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