【问题标题】:Why different outcomes with file-io on Windows and Linux machines when appending to file?为什么附加到文件时在 Windows 和 Linux 机器上使用 file-io 会产生不同的结果?
【发布时间】:2014-03-12 18:42:50
【问题描述】:

我有一个类,它通过读取上传的文件将新配置附加到现有配置。问题是它在 Windows 上运行良好,但在 Linux 上却不是这样——我使用 Servlet 来接收文件。新配置必须从一个新行开始,并且在任何地方都不能有空行。以下是代码。

    public class ConfigGen {

        public static void process(File configFile, File uploadedFile)
                throws IOException {
            synchronized (configFile) {
                if (shouldAppend(configFile, uploadedFile)) {
                    StringBuilder builder = readFile(uploadedFile);
                    StringBuilder orig = readFile(configFile);
                    FileWriter writer = new FileWriter(configFile, true);
                    // If the content ends with a new line character then remove the
                    // new line from content to be appended
                    if (orig.toString().endsWith(
                            System.getProperty("line.separator"))) {
                        writer.append(builder.substring(1));
                    } else {
                        writer.append(builder);
                    }
                    writer.flush();
                    writer.close();
                }
            }
        }

        /**
         * @param configFile
         * @param uploadedFile
         * @return true if config change is required, false otherwise
         * @throws IOException
         */
        private static final boolean shouldAppend(File configFile, File uploadedFile)
                throws IOException {
            synchronized (configFile) {
                String originalConfig = readFile(configFile).toString().trim();
                String newAddition = readFile(uploadedFile).toString().trim();
                String newSecGroup = newAddition.substring(0,
                        newAddition.indexOf(":"));
                return !originalConfig.contains(newSecGroup);
            }
        }

        private static final StringBuilder readFile(File file) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str = "";
            StringBuilder builder = new StringBuilder();
            while ((str = br.readLine()) != null) {
                builder.append(System.getProperty("line.separator") + str);
            }
            br.close();
            return builder;
        }
    }

添加新配置的文件通常会使用 vim 或类似的编辑器手动编辑。我得到的行为是 - 在 linux 机器上。 如果文件已使用 vim 编辑过,则新文本将附加在新行之后,此后任何后续更新都会从下一行出现 - 直到使用 vim 编辑。

例如:假设我在文件中有以下内容 - 最初使用 vim 编写。

sg-e696a08c:
  classes:
    tomcat6:
    myappmodule:
      endpoint: "10.0.2.11"
      port: "1443"

并且发布的新文件包含以下内容

sg-fde2a89d:
  classes:
    tomcat7:
    servmod:
      db: "xyz.com"
      port: "1336"

配置文件的预期内容是。

sg-e696a08c:
  classes:
    tomcat6:
    myappmodule:
      endpoint: "10.0.2.11"
      port: "1443"
sg-fde2a89d:
  classes:
    tomcat7:
    servmod:
      db: "xyz.com"
      port: "1336"

但是我得到了

sg-e696a08c:
  classes:
    tomcat6:
    myappmodule:
      endpoint: "10.0.2.11"
      port: "1443"

sg-fde2a89d:
  classes:
    tomcat7:
    servmod:
      db: "xyz.com"
      port: "1336"

请注意有一个空行。在使用 vim 编辑文件之前,发布的任何后续文件都不会产生任何空的新行。我不确定为什么我会出现这种行为。 windows的结果很好。由于这种行为,生成的配置变得无效。在 Windows 上,我使用 Notepad++ 来编辑文件。

vim 在我可能丢失的文件末尾添加了一些特殊字符还是其他?我应该怎么做才能解决这个问题,为什么会出现这种行为?

【问题讨论】:

  • 这是由于“换行”/“行尾”含义的“DOS”和“Unix”约定不同。看到这个问题:stackoverflow.com/questions/19240082/…
  • 尝试删除条件(orig.toString().endsWith(),只使用writer.append(builder.trim());
  • 使用writer.append(builder.trim());在使用vim编辑后第一次上传是正确的,但在下次上传时,文本从第一次上传后结束的同一行开始。
  • vim 会自动在文件末尾添加一个新行,如果原来没有的话。查看 cmets,您似乎不希望文件末尾有新行。

标签: java linux windows vim file-io


【解决方案1】:

在 readFile() 中不必要地添加了换行符:

//builder.append(System.getProperty("line.separator") + str);
builder.append(str).append(System.getProperty("line.separator"));

注释代码(来自进程)似乎是多余的。

 //if (orig.toString().endsWith(
 //          System.getProperty("line.separator"))) {
 //  writer.append(builder.substring(1));
 //} else {
 //  writer.append(builder);
 //} 

 // just use this:
 writer.append(builder);

除此之外值得一提的是,用 Vim 编写的文件如果 fileformat=unix 以换行符结尾,如果 fileformat=dos 则以回车+换行符结尾。但是,在这种情况下,这应该无关紧要,因为您使用的是 trim()

【讨论】:

    猜你喜欢
    • 2021-07-16
    • 1970-01-01
    • 2014-01-28
    • 2017-02-21
    • 2015-06-30
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多