Java 公共 io FileUtils.writeStringToFile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23830053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:28:15  来源:igfitidea点击:

commons io FileUtils.writeStringToFile

javafileutils

提问by DaeYoung

I have a program written in Java reading two properties files - source.properties and destination.properties and write key/value pair of each line from source to destination. I decided to use FileUtils.writeStringToFile method from apache commons io api instead of PrintWriter or FileWriter from java standard api. What I found is only last line in the source file is overwritten to the destination file.

我有一个用 Java 编写的程序,读取两个属性文件 - source.properties 和 destination.properties 并写入从源到目标的每一行的键/值对。我决定使用 apache commons io api 中的 FileUtils.writeStringToFile 方法,而不是 java 标准 api 中的 PrintWriter 或 FileWriter。我发现只有源文件中的最后一行被覆盖到目标文件中。

contents of the source.properties
username=a
host=abc

contents of the destination.properties
host=abc

source.properties 的内容
username=a
host=abc

destination.properties 的内容
host=abc

  static void writeToFile(Map<String,String> map, String pathToFile)    {                                 
    Iterator<Map.Entry<String,String>> itr = map.entrySet().iterator();
    File path = new File(pathToFile);
    while(itr.hasNext())    {
        Map.Entry<String,String> pairs = (Map.Entry<String,String>)itr.next();
        FileUtils.writeStringToFile(path,pairs.getKey() + "=" + pairs.getValue());
    }
}

map contains key/value pairs from the source file. When I debugged the program, I was able to see while loop go through two times and map contained all the correct data and the method from FileUtils called two times and wrote each line of data from the source file.

map 包含来自源文件的键/值对。当我调试程序时,我能够看到 while 循环经过两次并且 map 包含所有正确的数据,并且 FileUtils 中的方法调用了两次并从源文件中写入每一行数据。

Can someone explain to me why I am getting aforementioned output?

有人可以向我解释为什么我得到上述输出吗?

[update]
I was able to achieve what I wanted in using PrintWriter.

[更新]
我能够使用 PrintWriter 实现我想要的。

采纳答案by tmarwen

You have to use the FileUtils#writeStringToFilewith a boolean argument set to trueto tell the utils method that it should append the Stringto the end of file and not overwite it.

您必须使用FileUtils#writeStringToFile带有设置为布尔参数的true来告诉 utils 方法它应该将 附加String到文件的末尾而不是覆盖它。

@Deprecated
public static void writeStringToFile(File file,
                            String data,
                            boolean append)
                          throws IOException

So you code should be as below:

所以你的代码应该如下:

static void writeToFile(Map<String,String> map, String pathToFile)
{                                 
  Iterator<Map.Entry<String,String>> itr = map.entrySet().iterator();
  File path = new File(pathToFile);
  while(itr.hasNext())    {
    Map.Entry<String,String> pairs = (Map.Entry<String,String>)itr.next();
    FileUtils.writeStringToFile(path,
      pairs.getKey() + "=" + pairs.getValue(), 
      true);// append rather than overwrite
  }
}

Sidenote: This method is deprecated and you should use the one with Charsetspecified in method signature.

旁注:此方法已弃用,您应该使用Charset方法签名中指定的方法。