简体   繁体   English

用Java覆盖文件

[英]Overwriting a File in Java

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;

The content of the text file is: 文本文件的内容为:

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

Then I will replace not_out to the current time because I'm making a time-in and time-out program. 然后我将not_out替换为当前时间,因为我正在制作一个超时和超时程序。 But the always output is something like this: 但是总输出是这样的:

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

It always copies my first record. 它总是复制我的第一条记录。

This is not an answer, and so is being posted as a "community wiki", but you absolutely need to do some basic debugging. 这不是答案,因此被发布为“社区Wiki”,但是您绝对需要进行一些基本的调试。 You should in the least do something like: 您至少应执行以下操作:

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

And again you should show us more code including the ti(...) and to(...) variable. 同样,您应该向我们展示更多代码,包括ti(...)to(...)变量。

Also, you should strive to only post well-formatted code when asking questions here, code with regular, well-placed indentations, with enough but not too much whitespace (especially not too many empty lines). 此外,您应该在此处提问时仅努力发布格式正确的代码,带有规则且位置适当的缩进的代码,并留有足够但不太多的空格(尤其是不要有太多的空行)。 You want to strive to make it easy for us to help you. 您想努力使我们很容易为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM