Java 在添加while循环后,Printwriter似乎没有输出到指定的文件

Java 在添加while循环后,Printwriter似乎没有输出到指定的文件,java,while-loop,printwriter,Java,While Loop,Printwriter,我正处于学校项目的早期阶段,已经遇到了一个bug。如果我只是使用for循环而不使用while循环导出数组列表内容,那么一切都可以正常工作,但是当我想使用while循环以便提供选项时,输出文本文件不包含任何内容,就好像程序只创建了一个空文本文件而不做任何事情一样。我很困惑 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList;

我正处于学校项目的早期阶段,已经遇到了一个bug。如果我只是使用for循环而不使用while循环导出数组列表内容,那么一切都可以正常工作,但是当我想使用while循环以便提供选项时,输出文本文件不包含任何内容,就好像程序只创建了一个空文本文件而不做任何事情一样。我很困惑

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.PrintStream;
import javax.swing.JOptionPane;
import java.util.Arrays;

public class studentProcessor
{
   public static void main(String[] args) throws FileNotFoundException
   {  
      PrintStream pr=System.out;
      String inputFileName = JOptionPane.showInputDialog("Input file:");
      String outputFileName = JOptionPane.showInputDialog("Output file:");
      File inputFile = new File(inputFileName);
      Scanner in = new Scanner(inputFile);
      PrintWriter out = new PrintWriter(outputFileName);
      ArrayList<Student>student = new ArrayList<Student>();
      while (in.hasNextLine())
      {
          student.add(new Student(in.nextInt(), in.next(), in.nextDouble()));
      }
      in.close();
      boolean done=false;
      while (!done)
        {
            String m=JOptionPane.showInputDialog("Please enter the number for the action which you would like to perform." +
                "\n1. Add new student(s)\n2. Remove student(s)\n3. Update student's data\n4. Search for student" +
                "\n5. Sort students\n6. Create GPA report\n7. Output information to a .txt file\n0. Exit");
            if (m.equals("1"))
            {
                JOptionPane.showMessageDialog(null, "Add new student(s)");
            }
            else if (m.equals("2"))
            {
                JOptionPane.showMessageDialog(null, "Remove student(s)");
            }
            else if (m.equals("3"))
            {
                JOptionPane.showMessageDialog(null, "Update student's data");
            }
            else if (m.equals("4"))
            {
                JOptionPane.showMessageDialog(null, "Search for a student");
            }
            else if (m.equals("5"))
            {
                JOptionPane.showMessageDialog(null, "Sort students");
            }
            else if (m.equals("6"))
            {
                JOptionPane.showMessageDialog(null, "Create GPA report");
            }
            else if (m.equals("7"))
            {
                JOptionPane.showMessageDialog(null, "Output information to a .txt file");
                out.println("The students' information is listed below:");
                for (Student Stu:student)
                {
                    out.println(Stu.getId()+" "+Stu.getLName()+" "+Stu.getGpa());
                }
            }
            else if (m.equals("0"))
            {
                System.exit(0);
                done=true;
            }
            else 
            {
                JOptionPane.showMessageDialog(null, "You have not entered a valid command, try again.");
            }
        }
      out.close();
   }
}
谢谢你的帮助=]

编辑:这是现在使用printwriter的新代码部分。谢谢你的帮助

else if (m.equals("7"))
        {
            String outputFileName = JOptionPane.showInputDialog("Designate an output file for student information:");
            PrintWriter out = new PrintWriter(outputFileName);
            out.println("The students are listed below:");
            for (Student Stu:student)
            {
                out.println(Stu.getId()+" "+Stu.getLName()+" "+Stu.getGpa());
            }
            out.close();
        }

您是否在关闭PrintWriter之前尝试过刷新打印写入程序?

您是否在关闭PrintWriter之前尝试过刷新打印写入程序?

写入后,您需要刷新输出流:

out.flush();
此外,当用户选择写入选项(7)时,您将希望创建输出流。否则,您将重复将记录写入同一文件。更像这样:

     } else if (m.equals("7")) {
        JOptionPane.showMessageDialog(null,
              "Output information to a .txt file");
        final PrintWriter out = new PrintWriter(outputFileName);
        out.println("The students' information is listed below:");
        for (Student Stu : student) {
           out.println(Stu.getId() + " " + Stu.getLName() + " "
                 + Stu.getGpa());
        }
        out.close();
     } 

写入输出流后,需要刷新输出流:

out.flush();
此外,当用户选择写入选项(7)时,您将希望创建输出流。否则,您将重复将记录写入同一文件。更像这样:

     } else if (m.equals("7")) {
        JOptionPane.showMessageDialog(null,
              "Output information to a .txt file");
        final PrintWriter out = new PrintWriter(outputFileName);
        out.println("The students' information is listed below:");
        for (Student Stu : student) {
           out.println(Stu.getId() + " " + Stu.getLName() + " "
                 + Stu.getGpa());
        }
        out.close();
     } 

问题是永远不会调用带有out.close的行

System.exit方法在关闭打印写入程序之前终止应用程序

您应该使用try finally块来关闭资源


另外,如果System.exit调用位于方法的末尾,而不是在while循环中,则代码会更清晰。您已经在使用布尔值来终止循环,因此让它终止,然后继续执行代码的其余部分。

问题是,永远不会调用带有out.close的行

System.exit方法在关闭打印写入程序之前终止应用程序

您应该使用try finally块来关闭资源


另外,如果System.exit调用位于方法的末尾,而不是在while循环中,则代码会更清晰。您已经在使用布尔值终止循环,因此让它终止,然后继续执行代码的其余部分。

为什么要使用System.exit(0);将done设置为false将导致程序终止,并希望正确关闭输出文件。为什么有System.exit(0);将done设置为false将导致程序终止,并希望正确关闭输出文件。关闭将刷新写入程序,问题是没有调用close方法。关闭将刷新写入程序,问题是没有调用close方法。感谢您指出系统。exit()我改为布尔值后忘了删除它。我还解决了我的问题,并在循环中正确地初始化输出文件的字符串,而不是在程序开始时使用它。感谢您指出system.exit()我在更改为boolean后忘记删除它。我还解决了我的问题,并在循环中正确地初始化输出文件的字符串,而不是在程序开始时使用它。为了从另一个答案继续我的评论,我将out.close()移到了您显示的位置,现在它工作得很好。idk,如果它只是我放置out.close的地方,或者因为我也移动了out文件的字符串的初始化。我将发布一篇我所做的编辑文章,我想问,在PrintWriter之前的“final”是否会对代码的运行方式产生巨大的影响?对不起,我是个新手,这是我第一次尝试这么复杂的程序。用final标记变量可以给你的程序带来提升,因为它允许编译器进行某种优化。如果你不考虑它,它也不是一个杀手。为了从另一个答案继续我的评论,我将.close()移到了你显示的位置,现在效果很好。idk,如果它只是我放置out.close的地方,或者因为我也移动了out文件的字符串的初始化。我将发布一篇我所做的编辑文章,我想问,在PrintWriter之前的“final”是否会对代码的运行方式产生巨大的影响?对不起,我是个新手,这是我第一次尝试这么复杂的程序。用final标记变量可以给你的程序带来提升,因为它允许编译器进行某种优化。如果你不考虑它,它就不是杀手。