2014-01-29 126 views
1
try { 
    BufferedReader br = new BufferedReader(new FileReader(user + ".txt")); 
    String time = t.toString(); 

    while ((line = br.readLine()) != null) { 
     String [] check = line.split(";"); 
     date.add(check[0]); 
     timeIn.add(check[1]); 
     timeOut.add(check[2]);   
    } 
    br.close(); 

    BufferedWriter bw = new BufferedWriter(new FileWriter(user + ".txt")); 

    if (timeOut.contains("not_out")){ 
     indx = timeOut.indexOf("not_out"); 
     timeOut.set(indx, time);  
    } 

    for (int i = 0; i < date.size(); i++) { 
     d =getDate(i); 
     ti = ti(i); 
     to = to(i); 
     bw.write(d + ";" + ti + ";" + to); 
     bw.newLine(); 
    }   
    bw.close(); 

} catch (Exception e) { 
    System.out.println("Time out error"); 
    e.printStackTrace(); 
} 
return true; 

文本文件的內容是:覆蓋文件中的Java

eg. 11/22/13;8:00;8:30 
    11/23/13;8:00;not_out 

然後我將取代not_out到當前的時間,因爲我正在做的時間在和超時程序。 但總是輸出是這樣的:

11/22/13;8:00;8:30 
    11/22/13;8:00;8:30 
    11/23/13;8:00;8:40 

它總是複製我的第一個記錄。

+0

是你在閱讀後正確關閉文件?這似乎是封閉無處... – Learner

+3

這是一個非常不完整的問題,你留下太多洞讓我們能夠提供幫助。 'ti(i)'做什麼? 「(我)」做了什麼?他們持有什麼?你有沒有調試過這些傢伙?你爲什麼不使用你已經填充的timeIn和timeOut集合?至少你應該加入一些println語句來查看你的問題在哪裏。 –

+0

那麼ti(i)是一種獲取數組內部元素的方法timeIn中的(i)就像ti方法,但它獲取數組列表中的元素timeOut。 – user3194331

回答

1

這不是一個答案,所以被張貼爲「社區維基」,但你絕對需要做一些基本的調試。你應該在至少這樣做:

try{ 

    BufferedReader br = new BufferedReader(new FileReader(user+".txt")); 

    String time = t.toString(); 

    while((line=br.readLine())!=null){ 
     String [] check = line.split(";"); 
     System.out.println("check: " + java.util.Arrays.toString(check)); 
     date.add(check[0]); 
     timeIn.add(check[1]); 
     timeOut.add(check[2]); 
    } 
    br.close(); 


    BufferedWriter bw = new BufferedWriter(new FileWriter(user+".txt")); 


    if(timeOut.contains("not_out")){ 
     indx = timeOut.indexOf("not_out"); 
     timeOut.set(indx, time);  
    } 

    for (int i = 0 ; i <date.size();i++){ 
     d =getDate(i); 
     ti= ti(i); 
     to= to(i);  

     // ***** added ***** 
     System.out.printf("i: %d, d: %s, ti: %s, to: %s%n", i, d, ti, to); 
     bw.write(d+";"+ti+";"+to); 
     bw.newLine(); 
    }   
    bw.close(); 
    } catch(Exception e){ 
    System.out.println("Time out error"); 
    e.printStackTrace(); 
    } 
    return true; 
} 

並再次,你應該向我們展示更多的代碼,包括ti(...)to(...)變量。當你在這裏問問題時,你應該努力只發布格式良好的代碼,並且有規則的,有條理的縮進,有足夠的但不太多的空白(尤其是不要太多的空行)。你想努力使它容易爲我們提供幫助。

+0

我認爲這不是覆蓋是問題,感謝氣墊船在您的意見是啊我意識到問題是在我的arraylist我以前的數據不會被刪除,這就是爲什麼他們被列入我的寫作。現在我想我應該先清理我的數組列表,然後再放置元素 – user3194331