从文本文件中读取时如何维护EOL字符? [英] How do I maintain EOL characters when reading a from a text file?

查看:564
本文介绍了从文本文件中读取时如何维护EOL字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BufferedReader.readLine()自动删除EOL字符,我不能简单地执行readLine(),然后在它的末尾添加\ r。我试过了

BufferedReader.readLine() removes EOL characters automatically, and I cannot simply do a readLine() and then tack a "\r" on the end of it. I tried

InputStream myFile = new FileInputStream("C:\\test.txt");
StringBuilder sb = new StringBuilder();

int i;

while((i = myFile.read()) != -1)
{
    char ch = (char) i;
    sb.append(ch);
}

System.out.println(sb);

但是char ch =(char)i丢失了字节数据,因为ints是4个字节而chars是2个字节。

but the "char ch = (char) i" loses byte data because ints are 4 bytes while chars are 2 bytes.

我再说一遍,我不能做类似的事情

I repeat, I cannot do something like

sb.append(ch+"\r");

因为此通用代码将读取的某些文件将包含CR而其他文件将不会。

because some files that this generic code will read will include the CR and others will not.

来自java.nio。*,Files.readAllBytes(路径路径)似乎是一个选项。但是我不熟悉它并且无法判断它是否根据Javadoc返回EOL字符

From java.nio.*, Files.readAllBytes(Path path) seem like an option. But I am unfamiliar with it and cannot tell if it returns EOL characters or not based off the Javadoc

推荐答案

你理想情况下不要触摸字节。例如

You ideally don't touch the bytes. E.g.

public static String fromFile(File file, Charset charset) throws IOException {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
        StringWriter out = new StringWriter();
        char[] cbuf = new char[8192];
        int read;
        while ((read = reader.read(cbuf)) != -1) {
            out.write(cbuf, 0, read);
        }
        return out.toString();
    }
}

将所有内容直接转换为单个字符串。将 byte 转换为 char 确实很危险,除非你知道它只是ascii,否则你不应该自己尝试。让内置的字符集做到这一点。已经很难使用正确的。

Converts everything straight into a single String. Converting byte to char is indeed dangerous and you should not try to do that yourself unless you know it's only ascii. Let the builtin charsets do that. It's tricky enough to use the right one already.

Files.readAllBytes()确实会返回EOL字符on bytes并且不会尝试解释这些字节的含义。

Files.readAllBytes() does return EOL characters as it works on bytes and does not try to interpret what those bytes mean.

public static String fromPath(Path path, Charset charset) throws IOException {
    byte[] bytes = Files.readAllBytes(path);
    return new String(bytes, 0, bytes.length, charset);
}

是使用nio方法的等价物。使用 Paths.get(myfile.txt)调用,而不是使用新文件(myfile.txt)

is the equivalent using the nio methods. Call with Paths.get("myfile.txt") instead of with new File("myfile.txt").

这篇关于从文本文件中读取时如何维护EOL字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆