簡體   English   中英

從文件讀寫問題

[英]Issue with writing and reading from file

我處於一種情況,我想寫一些東西(用打孔和打孔方式寫員工詳細信息)並讀回文件(以顯示所有員工打孔和打孔的人的報告)。

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

}

在第[75]行,bw.write(content)我正在寫一個名為“ output.txt”的文件(我也想給我寫文件的那些雇員加上時間戳)。 但是由於某種原因,數據並沒有進入文件,所以我確信在關閉該文件時我在某個地方犯了一個錯誤,並且我想從我編寫的同一文件中讀取數據。 有人可以建議我哪里出問題了嗎?

添加更多詳細信息:

    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)

因此,當我現在轉到PC的E驅動器時,我只看到一個名為output.txt(最新修改時間)的文件,但其中沒有任何內容。 我試圖在循環后關閉緩沖區,但是沒有運氣。 另外,請建議您閱讀我寫入文件的數據

請指教!

謝謝

我不確定我是否理解您的問題,但是像這樣,您只能寫入一次文件,因為在寫入文件后將其關閉。 相反,您可以使用flush()方法來確保將內容寫入文件中。

考慮閱讀http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html上的文檔。

在我的測試中,它工作正常。 但是改變bw.close(); 到bw.flush(); 因為在第一次輸入后輸出流將關閉。 並在第二次輸入時遇到了異常

馬上,我建議嘗試用E:\\output.txt替換您的文件名。 我相信Windows文件路徑使用反斜杠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM