簡體   English   中英

我無法弄清楚為什么用數組覆蓋文件的內容導致空文件

[英]I can't figure out why overwriting a file's contents with an array results in an empty file

所以我修復了覆蓋問題,但現在問題是它吃掉了文本文件的最后一行。 或者我想它只是沒有把它寫回文件。 有任何想法嗎? 第一種方法應該將密碼更改為隨機密碼,第二種方法只是用於計算文本文件中的行數。

/**
 *  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();
   }
}

我懷疑你countLines()方法正在重新讀取文件以確定那一刻的行數。 這不僅非常低效,而且還是您的bug的來源。

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

countLines()如何工作?

我對這種方法有點懷疑,因為你正在使用它的結果迭代你的數組。 我本以為使用它的長度迭代數組可能更直觀(也更正確?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM