Java:将字符串从一个文件复制到另一个文件而不丢失“换行格式”;
如果标题有误导性/错误的话,提前抱歉,但这是我在花了很长时间练习 Java 后能做的最好的事情。 (我的大脑正在融化)
我将这段代码放在一起来读取一个文件并将其复制到另一个文件中,跳过以给定字符串开头的行(BeginOfTheLineToRemove)。它实际上可以工作并删除所需的行,但是由于某种原因,它忘记了 \n (换行符)。复制间距和符号。我想不通。我真的希望有人能帮忙。来自意大利的 Java 新手的欢呼;)
public void Remover(String file, String BeginOfTheLineToRemove) {
File StartingFile = new File(file);
File EndingFile = new File(StartingFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(EndingFile));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith(LineToRemoveThatBeginWithThis)) {
continue;
}
pw.write(line);
}
pw.close();
br.close();
}
Sorry in advance if the title is misleading/wrong but this is the best I can do after a really long day spent practicing with Java. (my brain is melting)
I put this code togheter to read a file and copy it into another file, skipping the line/lines that begins with a given string (BeginOfTheLineToRemove). It actually works and remove the desired line, but, for some reason, it forgets about the \n (newline). Spacing and symbols are copied. I can't figure it out. I really hope someone will help. cheers from a java newb from italy ;)
public void Remover(String file, String BeginOfTheLineToRemove) {
File StartingFile = new File(file);
File EndingFile = new File(StartingFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(EndingFile));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith(LineToRemoveThatBeginWithThis)) {
continue;
}
pw.write(line);
}
pw.close();
br.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
pw.println
而不是pw.write
。 println 在写入内容后添加换行符。Use
pw.println
instead ofpw.write
. println adds new line character after it writes content.您正在使用 PrintWriter.write() 来写入行 - 默认情况下不会在末尾写入换行符。请改用
println()
。You are using
PrintWriter.write()
to write the lines - This does not by default write newline at the end. Useprintln()
instead.这个可能会帮助你。
BufferedReader.readLine() 方法不读取任何行终止字符。因此,您的行将不包含任何终止字符。
This will probably help you.
The BufferedReader.readLine() method does not read any line termination characters. So therefore your line will not contain any termination characters.
BufferedReader#readLine文档说:
也就是说,读取器会从字符串中删除行终止字符,因此您需要手动添加他们再次:
BufferedReader#readLine documentation says:
That is, the reader strips the line termination characters from your Strings, so you need to manually add them again:
BufferedReader.readLine() 使用换行符来标识行尾,并且它返回的字符串不包含该换行符。换行符是分隔符,因此它不被视为数据的一部分。
为了弥补这一点,您可以在输出中添加换行符,如下所示:
在写出文本行后,对 PrintWriter.println() 的额外调用将打印换行符。
BufferedReader.readLine() uses the newline to identify the end of the line, and the string that it returns does not contain this newline. The newline is a separator, so it is not considered part of the data.
To compensate for this, you can add a newline to your output, like so:
The extra call to PrintWriter.println() will print a newline after you write out your line of text.
在循环之外获取系统的行分隔符:
然后将其附加到您读入的行:
Outside the loop get the system's line seperator:
Then append that to the line you've read in: