我无法弄清楚为什么用数组覆盖文件的内容导致空文件

时间:2012-09-14 08:31:49

标签: java arrays for-loop io printwriter

所以我修复了覆盖问题,但现在的问题是它吃掉了文本文件的最后一行。或者我想它只是没有把它写回文件。有任何想法吗?第一种方法应该将密码更改为随机密码,第二种方法只是用于计算文本文件中的行数。

/**
 *  Method for resetting a password
 * Replaces current password with randomly generated one.
 * 
 *  @param testUserName is the username used for finding the password
 *       to replace
 */
public String resetPassword(String testUserName) throws IOException  {
    int fileLength = countLines();
    FileReader fr = new FileReader("Password.txt");
    BufferedReader br = new BufferedReader(fr);
    String[] storeData = new String[fileLength];
    Random rand = new Random();
    int pwNumber = (int)(rand.nextDouble() * 10000);
    String pwReset = "Password" + pwNumber;

    for (int i=0; i < fileLength; i ++) {
        storeData[i] = br.readLine();
    }
    fr.close();     

    for (int i=0; i < (fileLength-1); i += 4) {
        if (testUserName.equals(storeData[i])) {
            storeData[i+1] = pwReset;
        }   
    }
    PrintWriter reset = new PrintWriter("Password.txt");
    for (int i=0; i < fileLength; i ++) {
        reset.println(storeData[i]);
    }
    reset.close();
    return pwReset;
}

/**
 *  Method for counting number of lines in a file
 *  Used in conjuction with other methods for reading
 * specific lines of files.
 */
public int countLines() throws IOException {
   InputStream is = new BufferedInputStream(new FileInputStream("Password.txt"));
   try {
      byte[] c = new byte[1024];
      int count = 0;
      int readChars = 0;
      boolean empty = true;
      while ((readChars = is.read(c)) != -1) {
         empty = false;
         for (int i = 0; i < readChars; ++i) {
            if (c[i] == '\n')
               ++count;
        }       
      }
      return (count == 0 && !empty) ? 1 : count;
    } finally {
      is.close();
   }
}

2 个答案:

答案 0 :(得分:2)

我怀疑你countLines()方法正在重新读取文件以确定那一刻的行数。这不仅非常低效,而且也是您的错误来源。

PrintWriter reset = new PrintWriter("Password.txt");
// file is now truncated.
countLine() == 0

我建议你计算一次的行数。理想情况下,当你阅读一次。


您可以通过

进行改进
  • 阅读文件一次。
  • 不将文件存储在内存中,因此它无关紧要。
  • 如果无法写入文件,则原始文件不会受到影响。

public String resetPassword(String testUserName) throws IOException {
    File passwdFile = new File("Password.txt");
    BufferedReader br = new BufferedReader(new FileReader(passwdFile));
    File tmpPasswdFile = new File("Password.txt.tmp");
    PrintWriter reset = new PrintWriter(tmpPasswdFile);
    String pwReset = null;
    try {
        boolean resetNextLine = false;
        for (String line; (line = br.readLine()) != null; ) {
            if (resetNextLine) {
                Random rand = new Random();
                int pwNumber = (int) (rand.nextDouble() * 10000);
                pwReset = "Password" + pwNumber;
                reset.println(pwReset);
            } else {
                reset.println(line);
            }
            resetNextLine = testUserName.equals(line);
        }
    } finally {
        reset.close();
        br.close();
    }
    passwdFile.delete();
    tmpPasswdFile.renameTo(passwdFile);
    return pwReset;
}

答案 1 :(得分:0)

countLines()如何运作?

我对这种方法有点怀疑,因为你正在使用它的结果迭代你的数组。我原以为使用 长度迭代数组可能更直观(也更正确?)。