Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我能';我不明白为什么要覆盖文件';数组中的内容将生成一个空文件_Java_Arrays_For Loop_Io_Printwriter - Fatal编程技术网

Java 我能';我不明白为什么要覆盖文件';数组中的内容将生成一个空文件

Java 我能';我不明白为什么要覆盖文件';数组中的内容将生成一个空文件,java,arrays,for-loop,io,printwriter,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 th

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

/**
 *  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();
   }
}
/**
*重置密码的方法
*将当前密码替换为随机生成的密码。
* 
*@param testUserName是用于查找密码的用户名
*取代
*/
公共字符串重置密码(字符串testUserName)引发IOException{
int fileLength=countLines();
FileReader fr=新的FileReader(“Password.txt”);
BufferedReader br=新的BufferedReader(fr);
String[]storeData=新字符串[fileLength];
Random rand=新的Random();
int pwNumber=(int)(rand.nextDouble()*10000);
字符串pwReset=“Password”+pwNumber;
for(int i=0;i
countLines()如何工作

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

我怀疑您countLines()方法正在重新读取文件以确定此时的行数。这不仅效率很低,而且是bug的根源

PrintWriter reset = new PrintWriter("Password.txt");
// file is now truncated.
countLine() == 0
我建议你数一次行数。理想的情况是你读一次


你可以通过

  • 读取文件一次
  • 不将文件存储在内存中,因此不管文件有多大
  • 如果它无法写入文件,则原始文件将保持不变


您可以显示您的countLines方法吗?在这两者之间是否发生任何异常?password.txt文件是一个预先存在的文件,如果用户注册,它可能会更改。countLine()统计文件中的条目数。如果您的应用程序是多线程的,那么在您处理resetPassword?时,其他线程可能会修改您文件的内容,并将其设置为等于一个变量,然后在其余时间使用该变量?或者更好地使用集合,如ArrayList,然后,当您逐行读取文件时,将它们添加到ArrayList中。然后在最后你有你的行计数和文件内容。然后可以使用迭代器对其进行迭代。@Adam6806如果查看示例代码,则根本不需要将数据存储在内存中。;)我会检查文件长度是否正确。您可以打印出该值以查看其是否正确。同样,我会使用调试器来检查为什么它是错误的(假设它是错误的)@Adam6806如果你没有IDE,我建议你使用一个。如果这样做,请通过单击该行(通常在该行的左侧)并按
Debug
而不是
Run
将rbeakpoint添加到方法的开头,这样您就可以从断点逐步执行程序。
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;
}