简体   繁体   English

如何一次读取文件x字节? 在java中

[英]How do I read file x bytes at a time ? in java

I want to read a file into a String in Java, x chars at a time. 我想将文件读入Java的String中,一次x个字符。 Then I'll do something with string, and want to continue from where I left off. 然后,我将对字符串进行一些操作,并希望从我中断的地方继续。 How do I go about it ? 我该怎么办?

edit : 编辑:

Target file is a simple text file. 目标文件是一个简单的文本文件。

Well, firstly you need to differentiate between bytes and characters . 好吧,首先您需要区分字节字符 You can read from an InputStream a certain number of bytes at a time (as a maximum number; there's no guarantee that you'll be given all the bytes that you ask for) and you can read from a Reader a number of characters at a time (again, as a maximum). 您可以一次从InputStream读取一定数量的字节(作为最大数量;不能保证将获得所需的所有字节),并且可以从Reader一次读取多个字符。时间(再次,最大)。

It sounds like you probably want to use an InputStreamReader around an InputStream , specifying the appropriate character encoding, and then read from the InputStreamReader . 听起来您可能想在InputStreamReader周围使用InputStream ,指定适当的字符编码,然后从InputStreamReader读取。 If you have to have an exact number of characters, you'd need to loop round - for example: 如果必须有准确的字符数,则需要循环-例如:

public static String readExactly(Reader reader, int length) throws IOException {
    char[] chars = new char[length];
    int offset = 0;
    while (offset < length) {
        int charsRead = reader.read(chars, offset, length - offset);
        if (charsRead <= 0) {
            throw new IOException("Stream terminated early");
        }
        offset += charsRead;
    }
    return new String(chars);
}

Have you tried using BufferedReader ? 您是否尝试过使用BufferedReader It defines read(char[], int, int) that does pretty much exactly what you want. 它定义了read(char[], int, int) ,它几乎完全满足您的要求。 That is, it recursively calls read in an attempt to fill the buffer. 也就是说,它递归地调用read来尝试填充缓冲区。

Example usage: 用法示例:

char[] chars = new char[length];
reader.read(chars,0,length);
String str = String.valueOf(chars);

Documentation: 说明文件:

public int read(char[] cbuf, int off, int len) throws IOException public int read(char [] cbuf,int off,int len)抛出IOException

This method implements the general contract of the corresponding read method of the Reader class. 此方法实现Reader类的相应read方法的常规协定。 As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method of the underlying stream. 作为一个额外的便利,它尝试通过重复调用基础流的读取方法来读取尽可能多的字符。 This iterated read continues until one of the following conditions becomes true: 重复执行此迭代读取,直到满足以下条件之一:

  • The specified number of characters have been read, 已读取指定数量的字符,
  • The read method of the underlying stream returns -1, indicating end-of-file, or 基础流的read方法返回-1,表示文件结束,或者
  • The ready method of the underlying stream returns false, indicating that further input requests would block. 基础流的ready方法返回false,指示其他输入请求将被阻止。

If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. 如果对基础流的第一次读取返回-1表示文件结束,则此方法返回-1。 Otherwise this method returns the number of characters actually read. 否则,此方法返回实际读取的字符数。

The last point is important as it means that streams that can block might return with less characters than expected. 最后一点很重要,因为这意味着可以阻塞的流返回的字符可能少于预期的字符。 Though when reading from a local file a reader or stream should always have bytes available to read. 尽管从本地文件读取时,读取器或流应始终具有可读取的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM