Java 从文件中写入和读取的问题

Java 从文件中写入和读取的问题,java,bufferedreader,Java,Bufferedreader,我现在的处境是,我想写一些东西,用punchin&punchout将员工的详细信息写入文件,并将其读回,以显示所有员工的记录 package Test; import java.io.*; import java.text.*; import java.util.*; public class Test { public static void main(String[] args) throws IOException { String lastName = "";

我现在的处境是,我想写一些东西,用punchin&punchout将员工的详细信息写入文件,并将其读回,以显示所有员工的记录

package Test;

 import java.io.*;
 import java.text.*;
 import java.util.*;
 public class Test {

public static void main(String[] args) throws IOException {
    String lastName = "";
    String firstName = "";
    String choice = "y";
    String customerChoice = "x";
    int empid = 0;
    Scanner sc = new Scanner(System.in);

    int currentIndex;

    File file = new File("E:/output.txt");
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    int[] punchedArray;
        punchedArray = new int[100];

   // System.out.println(t);
    while (!customerChoice.equalsIgnoreCase("d") && customerChoice != "invalid" && choice.equalsIgnoreCase("y")) {
        customerChoice = getValidCustomerChoice(sc);
        if (customerChoice.equalsIgnoreCase("a")) {
           // System.out.println("In Create Customer");
            System.out.print("Enter your first name: ");

                firstName = sc.next();
                System.out.print("Enter your last name: ");
                lastName = sc.next();
                System.out.println(firstName + lastName + "with employee id" + empid);
                //System.out.println(firstNameArray[newEmployeeIndex] + " " + lastNameArray[newEmployeeIndex] + " your employee ID is: " + employeeIDArray[newEmployeeIndex]);
                empid++;

            //sc.next();
        }
        if (customerChoice.equalsIgnoreCase("b")) {
            System.out.println("Welcome to the punch in/out screen"+"\n");
                System.out.print("Enter your employee ID: ");
                currentIndex = Integer.parseInt(sc.next());

                if (punchedArray[currentIndex] == 0)
                {
                    System.out.println("You are now punched in");
                    punchedArray[currentIndex] = 1;

                }
                else if (punchedArray[currentIndex] == 1)
                {
                    System.out.println("You are now punched out");
                    punchedArray[currentIndex] = 0;
                }
               System.out.print(firstName + " "+ lastName + " "+ "your employee ID is " + currentIndex + " and your clock date and time is: " + " "+ cal.getTime() +"\n");
               String content = firstName + lastName + empid + cal.getTime();
               bw.write(content);
               bw.newLine();
               bw.close();
        }

        if (customerChoice.equalsIgnoreCase("c")) {
            System.out.print("Welcome to the report screen." + "\n");
            System.out.print("Enter your selection (I = individual report or A= all employees):" + "\n");
            customerChoice = sc.next();
            if (customerChoice.equalsIgnoreCase("i")) {
                System.out.println("In Individual Report");
            } else if (customerChoice.equalsIgnoreCase("a")) {
                System.out.println("In Consoldated Report");
            }
        }
        if(customerChoice.equalsIgnoreCase("d"))
        {
        //bw.close();
            break;
        }

    }
}

public static String getValidCustomerChoice(Scanner sc) {
    String customerChoice = "";
    // sc.nextLine();

    boolean isValid = false;
    int invalidCounter = 0;
    System.out.print("Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):");
    while (isValid == false && invalidCounter < 3) {

        customerChoice = sc.next();
        if (!customerChoice.equalsIgnoreCase("a")
                && !customerChoice.equalsIgnoreCase("b") && !customerChoice.equalsIgnoreCase("c") && !customerChoice.equalsIgnoreCase("d") && !customerChoice.equalsIgnoreCase("I") && !customerChoice.equalsIgnoreCase("A")) {
            System.out.println("Invalid choice. Try again.\n");
            invalidCounter++;
        } else {
            isValid = true;
        }
        sc.nextLine();
    }
    if (invalidCounter >= 3) {
        System.out.println("Invalid three times. Program Exiting.\n");
        return "invalid";
    }
    return customerChoice;
}
所以,当我现在转到电脑的E盘时,我只看到一个名为output.txt latest modified time的文件,但里面什么都没有。我试图在循环后关闭缓冲区,但没有成功。另外,请告知如何读取我写入文件的数据

请告知


马上谢谢你,我建议用E:\output.txt替换你的文件名。我相信windows文件路径使用反斜杠。

我不确定是否理解您的问题,但像这样,您只能向文件写入一次,因为您在写入文件后将其关闭。相反,您可以使用flush方法来确保将内容写入文件


考虑阅读。

上的文档,在我的测试中,它运行良好。但是改变bw.close;冲洗;因为outputstream将在第一次输入后关闭。在第二次输入时,您遇到了异常

您能告诉我们复制问题的步骤吗?output.txt的一个示例?您关闭循环中的bufferedwriter。@grasGendarme将其作为答案发布please@Sandy在我的测试中效果很好。但是改变bw.close;冲洗;因为outputstream将在第一次输入后关闭。在第二次输入时,你得到一个exception@Sandy查找使用Filereader的示例。希望这有帮助。在windows下,您可以同时使用\`和/`并且它不会对基本文件路径产生任何影响,而在其他操作系统下是这样的
    run:
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):a
    Enter your first name: Sa
    Enter your last name: Ka
    SaKawith employee id0
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):b
    Welcome to the punch in/out screen

    Enter your employee ID: 0
    You are now punched in
    Sa Ka your employee ID is 0 and your clock date and time is:  Sun Jun 08 20:19:42 EDT 2014
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):a
    Enter your first name: Ka
    Enter your last name: Ma
    KaMawith employee id1
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):b
    Welcome to the punch in/out screen

    Enter your employee ID: 1
    You are now punched in
    Ka Ma your employee ID is 1 and your clock date and time is:  Sun Jun 08 20:19:42 EDT 2014
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):c
    Welcome to the report screen.
    Enter your selection (I = individual report or A= all employees):
    a
    In Consoldated Report
    Enter your selection (a= Add New Employee, b = Punch in/out, c= Report, d = Exit):BUILD STOPPED (total time: 1 minute 8 seconds)