【问题标题】:Terminate line without readLine() Method Java没有 readLine() 方法 Java 的终止行
【发布时间】:2015-09-26 14:30:40
【问题描述】:

作为我的Previous Question,我使用来自java.io.FileInputStreamread();read() 在同一行中提供输出,而不会终止行。之后,我使用来自java.io.BufferedReaderreadLine() 并在不同行中获得输出,行终止。我探索了readLine() 方法,我从Java API Docs 得到了这条线

读取一行文本。一条线被认为被任何人终止 换行符 ('\n')、回车符 ('\r') 或回车符 紧接着是换行符。

在阅读了 Java Docs 的上述引用之后,我修改了我的 read(); 方法程序以终止没有 readLine() 方法的行。但是失败了,

这是修改后的代码。(我想用于行终止)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BoxDemo {
    public static void main(String[] args) throws IOException {

        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;

            while ((c = in.read()) != -1) {
                if((char)c=='\r' || (char)c=='\n') {
                    out.write('\n');
                } else {
                    out.write(c);
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

这里是 readLine(); 方法代码,它给出了行终止的输出。

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class BoxDemo {
    public static void main(String[] args) throws IOException {

        BufferedReader inputStream = null;
        PrintWriter outputStream = null;

        try {
            inputStream = new BufferedReader(new FileReader("xanadu.txt"));
            outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                outputStream.println(l);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}

【问题讨论】:

  • 您遇到的错误是什么...?
  • @redflar3 我没有得到行终止的输出。输出仍然在一行中。
  • 您在哪里查看的文件?如果记事本,它不会将\n 识别为新行。在记事本++ 或任何高级编辑器中查看..

标签: java


【解决方案1】:

作为参考,这里是您的第一个代码的输出,它与您最后一个问题具有相同的 xanadu.txt 文件:

输入:

In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

输出:

In Xanadu did Kubla Khan

A stately pleasure-dome decree:

Where Alph, the sacred river, ran

Through caverns measureless to man

Down to a sunless sea.

所以某处有多余的换行符。这来自以下 if 条件:

if((char)c=='\r' || (char)c=='\n') {
    out.write('\n');

我猜您是在行终止符为 \r\n 的 Windows 机器上运行此程序,因此字符 \r\n 实际上都被读取,导致 \n 被写入两次。

同样,由于您似乎使用的是 Windows,根据您的编辑器,它可能不会将 \n 格式化为换行符并在一行中输出所有内容。

你可以通过写作来纠正这个问题

out.write('\r');
out.write('\n');

明确地。

话虽如此,我不建议您使用它。您应该尽可能使用System.lineSeparator(),因为这会导致系统相关的正确行分隔符。

【讨论】:

  • 参考输出只是由于 Stackoverflow,但在文本文件中它是一行。
  • 如何在write() 中使用System.lineSeparator()?因为我想在 txt 文件中终止行。
猜你喜欢
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2010-09-14
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
相关资源
最近更新 更多