2017-02-02 3 views
0
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(); 
} 

Как добавить текстовый файл, не затрагивая сохраненные данные в текстовом файле? это только отрывок моих кодов. Метод выше должен вводить время и время в текстовом файле каждый цикл, но он заменяет только сохраненные данные в текстовом файле. Это всегда появляется.Добавление к текстовому файлу при сохранении сохраненных данных в TextFile

DTR.txt pop-up

+0

A FileWr У iter' есть конструктор 'FileWriter (File file, boolean append)', где вам нужно установить 'append' в' true'. – IQV

+0

Вы всегда получите сообщение из текстового редактора Eclipse, если вы измените текстовый файл вне Eclipse, пока редактор открыт. –

ответ

2

Используйте FileOutpuStream для настройки потока, это позволит определить, что вы хотите добавить к текущему файлу

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

Посмотрите FileOutputStream для более deatils

Вы также можете взглянуть на try-catch-with-resources, который поможет сделать вашу жизнь проще