【问题标题】:Is there a way to tell java BufferedReader to read upto certain point有没有办法告诉 java BufferedReader 读取到某个点
【发布时间】:2023-03-29 01:51:01
【问题描述】:

我正在使用 Java BufferedReader 并使用 bufferedReader.readline() 逐行读取文件。

有没有一种方法可以让我知道该行有这么多文本并且不要再阅读它。

换句话说,是否已经存在可以查看行内容大小然后开始阅读的内容。那么它会读取到提供的大小吗?

【问题讨论】:

    标签: java bufferedreader


    【解决方案1】:

    this 是你的意思吗?

    public int read(char[] buf, 整数偏移量, 整数)

    地点:

    buf - 应该存储读取的字符的数组,偏移量 - 数组中开始存储字符的偏移量,计数 - 请求的 要读取的字符数

    【讨论】:

      【解决方案2】:

      您可以为此使用read(char[] buffer,int offet,int length)

      【讨论】:

        【解决方案3】:

        如果您知道一行的大小,则可以使用BufferedReader.read(char[] cbuf, int off, int len) 阅读您想要的内容,并使用BufferedReader.skip(long n) 跳过其余部分。

        您可以阅读您的行,然后将其截断为您期望的长度。

        String line = bufferedReader.readLine();
        String truncatedLine = line.substring(0, expectedLength);
        

        【讨论】:

          猜你喜欢
          • 2011-05-22
          • 1970-01-01
          • 2012-08-20
          • 1970-01-01
          • 1970-01-01
          • 2016-04-26
          • 1970-01-01
          • 1970-01-01
          • 2011-06-25
          相关资源
          最近更新 更多