简体   繁体   English

For循环似乎没有执行

[英]For loop doesn't seem to be executing

When I run this only one line is written to the file even when the ArrayList hash has more than one value. 当我运行它时,即使ArrayList散列具有多个值,也只会向文件写入一行。

I have tried removing fileWriter.close but then nothing is written to the file. 我已经尝试删除fileWriter.close但是没有任何内容写入该文件。

for (int i = 0; i < hash.size(); i++) {
    String fileContent = hash.get(i);
    FileWriter fileWriter;
    try {
        fileWriter = new FileWriter(OutputPath);
        fileWriter.write(fileContent);
        fileWriter.write("/n");
        fileWriter.close();
    } catch (IOException ex) {
        Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I would like this to write each value of the array to its own line in the file. 我希望这会将数组的每个值写入文件中自己的行。

You are continuously opening and closing the file; 您不断打开和关闭文件; you should open it once before your loop (and close it once after). 你应该在你的循环之前打开它一次(然后关闭它一次)。 However, it is not necessary to manually close the file at all if you use the try-with-Resources close statement (which is what I would prefer ). 但是,如果使用try-with-Resources close语句 (这是我更喜欢的 ),则根本不需要手动关闭文件。 And a newline is \\n (but that's also OS specific) so I would use System.lineSeparator() . 换行符是\\n (但这也是特定于操作系统的)所以我会使用System.lineSeparator() Finally, there is no reason to use an explicit array index here so I would use a for-each loop . 最后,没有理由在这里使用显式数组索引,因此我将使用for-each循环 Like, 喜欢,

try (FileWriter fileWriter = new FileWriter(OutputPath)) {
  for (String fileContent : hash) {
    fileWriter.write(fileContent);
    fileWriter.write(System.lineSeparator());
  }
} catch (IOException ex) {
    Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}

That's because you keep overwriting the same file. 那是因为你一直在覆盖同一个文件。 Every time you execute one iteration of the loop, you open the file and overwrite its contents. 每次执行循环的一次迭代时,都会打开文件并覆盖其内容。

Move your for loop inside the try/catch between the opening and closing of the file as follows: 在文件的打开和关闭之间移动try环境中的for循环,如下所示:

FileWriter fileWriter;
try {
    // open the file once
    fileWriter = new FileWriter(OutputPath);

    // loop through your items, writing each one to the file
    for (int i = 0; i < hash.size(); i++) {
        String fileContent = hash.get(i);
        fileWriter.write(fileContent);
        fileWriter.write("/n");
    }

    // close the file once
    fileWriter.close();
} catch (IOException ex) {   
    Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}

use fileWriter = new FileWriter(OutputPath, true); 使用fileWriter = new FileWriter(OutputPath, true); . This will make append to true. 这将使得追加到真实。 So your file content will not be overridden in the for loop 因此,您的文件内容不会在for循环中被覆盖

or move the fileWriter = new FileWriter(OutputPath); 或者移动fileWriter = new FileWriter(OutputPath); out from the for loop for loop

The for loop is executed and each time you overwrite the content of the file. 每次覆盖文件内容时都会执行for循环。

You should use append mode when opening a file stream. 打开文件流时应使用追加模式。 Otherwise you will overwrite all lines. 否则你将覆盖所有行。

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

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