2017-02-02 17 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

'FileWr iter'は、 'append'を' true'に設定しなければならないコンストラクタ 'FileWriter(File file、boolean append)'を持っています。 – IQV

+0

エディタが開いている間にEclipseの外部でテキストファイルを変更すると、Eclipseテキストエディタからメッセージが表示されます。 –

答えて

2

これは、よりdeatils

ためFileOutputStreamを見てくださいあなたは、現在のファイルに

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

を追加することを指定することができます、セットアップにストリームをFileOutpuStreamを使用しますまた、あなたの人生を楽にしてくれるtry-catch-with-resourcesをご覧ください。

関連する問題