我有一个类,它通过读取上传的文件将新配置附加到现有配置。问题是它在 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
在文件末尾添加了一些我可能丢失的特殊字符或其他字符?我应该做什么来解决这个问题,为什么会出现这种行为?
最佳答案
您在 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);
除此之外,可能值得一提的是,如果 fileformat=unix
则用 Vim 编写的文件以换行符结尾,如果 fileformat=dos
则以回车+换行符结尾。但是,在这种情况下这应该没有什么影响,因为您使用了 trim()
。
关于java - 为什么附加到文件时 Windows 和 Linux 机器上的 file-io 会产生不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360974/