簡體   English   中英

在將存儲的數據保留在TextFile中的同時追加到Textfile

[英]Appending to a Textfile while keeping the stored data in the TextFile

public static void InandOut() {


    Scanner scnThree = new Scanner(System.in);
    String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

    for(int iNum = 0; iNum < days.length; iNum++){
        System.out.printf("Enter time-in for %s: ", days[iNum]);
        timein = scnThree.nextLine();
        System.out.printf("Enter time-out for %s: ", days[iNum]);
        timeout = scnThree.nextLine();

        String strIn[] = timein.split(":");
        float intIn[] = new float[strIn.length];

        for(int i = 0; i < intIn.length; i++) {
            intIn[i] = Integer.parseInt(strIn[i]);
        }

        float minutesIn = intIn[1] / 60;
        float hoursIn = intIn[0] + minutesIn;

        String strOut[] = timeout.split(":");
        float intOut[] = new float[strOut.length];

        for(int i = 0; i < intOut.length; i++){
            intOut[i] = Integer.parseInt(strOut[i]);
        }

        float minutesOut = intOut[1] / 60;
        float hoursOut = intOut[0] + minutesOut;

        late = hoursIn - 8;
        undertime = 17 - hoursOut;
        dailyworkhours = 8 - (late + undertime);
        totalLate += late;
        totalUndertime += undertime;
        totalNumberofWorkHours += dailyworkhours;



        try{
            oFile = new Formatter("DTR.txt");
        }catch(Exception e){
            System.out.println("You've got an error!");
        }

        oFile.format("\n%s\t %s\t %s", days[iNum], timein, timeout);

        oFile.close();
    }

    System.out.print("Enter the coverage date of this payroll: ");
    coverDate = scnThree.nextLine();
}

如何在不影響文本文件中存儲數據的情況下追加到文本文件? 這只是我的代碼的一部分。 上面的方法應該在每個循環中在文本文件中輸入超時時間和超時時間,但它只會替換文本文件中存儲的數據。 這總是彈出。

DTR.txt彈出窗口

使用FileOutpuStream設置流,這將允許指定您想要附加到當前文件

    try (Formatter oFile = new Formatter(new FileOutputStream("DTR.txt", true))) {
        //...
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }

看看FileOutputStream的更多細節

您可能還想看看帶資源的try-catch-with資源 ,這將使您的生活更輕松

暫無
暫無

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

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