Java:将字符串从一个文件复制到另一个文件而不丢失“换行格式”;

发布于 2024-12-01 08:42:41 字数 784 浏览 0 评论 0原文

如果标题有误导性/错误的话,提前抱歉,但这是我在花了很长时间练习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妄想挽回 2024-12-08 08:42:41

使用 pw.println 而不是 pw.write。 println 在写入内容后添加换行符。

Use pw.println instead of pw.write. println adds new line character after it writes content.

娇纵 2024-12-08 08:42:41

您正在使用 PrintWriter.write() 来写入行 - 默认情况下不会在末尾写入换行符。请改用 println()

You are using PrintWriter.write() to write the lines - This does not by default write newline at the end. Use println() instead.

还给你自由 2024-12-08 08:42:41

这个可能会帮助你。

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.

感性不性感 2024-12-08 08:42:41

BufferedReader#readLine文档说:

返回:包含行内容的字符串,不包括任何行终止字符,如果已到达流末尾,则返回 null

也就是说,读取器会从字符串中删除行终止字符,因此您需要手动添加他们再次:

// \n on Linux/Mac, \r\n on Windows
String lineSep = System.getProperty("line.separator"); 
pw.write(line);
pw.write(lineSep);

BufferedReader#readLine documentation says:

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

That is, the reader strips the line termination characters from your Strings, so you need to manually add them again:

// \n on Linux/Mac, \r\n on Windows
String lineSep = System.getProperty("line.separator"); 
pw.write(line);
pw.write(lineSep);
梦途 2024-12-08 08:42:41

BufferedReader.readLine() 使用换行符来标识行尾,并且它返回的字符串不包含该换行符。换行符是分隔符,因此它不被视为数据的一部分。

为了弥补这一点,您可以在输出中添加换行符,如下所示:

while((line = br.readLine()) != null) {
    if(line.startsWith(LineToRemoveThatBeginWithThis)) continue;
    pw.write(line);
    pw.println();
}

在写出文本行后,对 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:

while((line = br.readLine()) != null) {
    if(line.startsWith(LineToRemoveThatBeginWithThis)) continue;
    pw.write(line);
    pw.println();
}

The extra call to PrintWriter.println() will print a newline after you write out your line of text.

妞丶爷亲个 2024-12-08 08:42:41

在循环之外获取系统的行分隔符:

String lineSeparator = System.getProperty("line.separator");

然后将其附加到您读入的行:

pw.write(line+lineSeparator);

Outside the loop get the system's line seperator:

String lineSeparator = System.getProperty("line.separator");

Then append that to the line you've read in:

pw.write(line+lineSeparator);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文