Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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
如何让matcher.find while循环写入文本文件?JAVA_Java_Filewriter - Fatal编程技术网

如何让matcher.find while循环写入文本文件?JAVA

如何让matcher.find while循环写入文本文件?JAVA,java,filewriter,Java,Filewriter,我使用while(matcher.find())循环将某些子字符串写入文件。在System.out控制台中,我可以获得文件中出现的匹配字符串列表,但当我尝试使用FileWriter写入文本文件时,我只能得到循环中最后写入的字符串。我在stackoverflow上搜索过类似的问题(它名副其实),但我找不到任何对我有帮助的东西。只是想澄清一下,这不是在EDT上运行的。有人能解释一下在哪里寻找问题吗 try { String writeThis = inputId1 + count + inp

我使用while(matcher.find())循环将某些子字符串写入文件。在System.out控制台中,我可以获得文件中出现的匹配字符串列表,但当我尝试使用FileWriter写入文本文件时,我只能得到循环中最后写入的字符串。我在stackoverflow上搜索过类似的问题(它名副其实),但我找不到任何对我有帮助的东西。只是想澄清一下,这不是在EDT上运行的。有人能解释一下在哪里寻找问题吗

try {
    String writeThis = inputId1 + count + inputId2 + link + inputId3;
    newerFile = new FileWriter(writePath);
    //this is only writing the last line from the while(matched.find()) loop
    newerFile.write(writeThis);
    newerFile.close();
    //it prints to console just fine!  Why won't it print to a file?
    System.out.println(count + " " + show + " " + link); 
    } catch (IOException e) {
        Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        try {
            newerFile.close();
            } catch (IOException e) {
                Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);

            }
    }
}
快速修复:

改变

newerFile = new FileWriter(writePath);

这将使用构造函数


更好的解决方案:

while(matcher.find())
循环之外创建
FileWriter
,然后将其关闭(或者将其用作
尝试资源初始化)

代码类似于:

try (FileWriter newerFile = new FileWriter(writePath)) {
   while (matcher.find()) {
      newerFile.write(matcher.group());
   }
} ...
快速修复:

改变

newerFile = new FileWriter(writePath);

这将使用构造函数


更好的解决方案:

while(matcher.find())
循环之外创建
FileWriter
,然后将其关闭(或者将其用作
尝试资源初始化)

代码类似于:

try (FileWriter newerFile = new FileWriter(writePath)) {
   while (matcher.find()) {
      newerFile.write(matcher.group());
   }
} ...

您不应该在每次循环迭代中创建
FileWriter
的实例。您需要在循环之前保留方法
write()
的用法,并在循环之后关闭init
FileWriter

您不应该在每次循环迭代中创建
FileWriter
的实例。您需要在循环之前保留方法
write()
和init
FileWriter
的用法,并在循环之后关闭它。

啊,是的,我在循环中创建FileWriter,没有附加开关lol。。。谢谢:)啊,是的,我在循环中创建FileWriter,没有附加开关lol。。。谢谢:)
Please check as follows:

FileWriter newerFile = new FileWriter(writePath);
while(matcher.find())
{
xxxxx
try {
    String writeThis = inputId1 + count + inputId2 + link + inputId3;

    //this is only writing the last line from the while(matched.find()) loop
    newerFile.write(writeThis);
    newerFile.flush();
    //it prints to console just fine!  Why won't it print to a file?
    System.out.println(count + " " + show + " " + link); 
    } catch (IOException e) {
        Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        try {
            newerFile.close();
            } catch (IOException e) {
                Logger.getLogger(Frame1.class.getName()).log(Level.SEVERE, null, e);

            }
    }
}
}