public static void doubleSpace(String fileName) {
try {
FileReader reader = new FileReader(fileName);
Scanner in = new Scanner(reader);
String outputFileName = fileName.charAt(0) + ".ds";
PrintWriter pOut = new PrintWriter(outputFileName);
// Opening of files for input and output
while (in.hasNextLine()) {
String line = in.nextLine();
pOut.println(line + "\n");
pOut.print("\n");
// System.out.println(line + "\n"); //Test
}
pOut.close(); // Close the files if they have been opened.
} catch (Exception e) {
}
}
所以基本上我的输入文件包含
a
b
c
我的输出文件应该是
a
b
c
但是,我的输出文件始终只包含abc
。
非常感谢任何帮助!
答案 0 :(得分:1)
使用BufferedWriter
。它有一个.newLine()
方法。此方法将使用平台的默认行分隔符。
使用BufferedReader
。它有一个.readLine()
方法。
示例:
// NOTE: you should really be using UTF-8
final Charset charset = Charset.defaultCharset();
final Path src = Paths.get(filename);
final Path dst = Paths.get(filename + ".ds");
String line;
try (
final BufferedReader reader = Files.newBufferedReader(src, charset);
final BufferedWriter writer = Files.newBufferedWriter(dst, charset);
) {
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
writer.newLine();
}
}
答案 1 :(得分:0)
您可能在平台的新线路上使用了错误的字符。使用
System.getProperty("line.separator");
获得正确的价值。