English 中文(简体)
BufferedReader有什么样的标记和重新设置?
原标题:What are mark and reset in BufferedReader?

她想知道<代码>代码()和reset(> 方法BufferedReader? 我如何利用它们? 我读了 Java,但作为开端人,我无法理解。

问题回答

mark/code> and reset methods of streams provide a way to pitbacks in the stream and re-read data.

当你在<条码>上打上<条码>符号><>>>时,将开始把从该点读到的数据放在内部缓冲。 当您打电话reset(>)时,将跳跃到上游的标志位置上,下台read(s>将由内层缓冲感到满意。 当你读过该缓冲地带的结束时,它将顺畅地重读新的数据。 <代码>BufferedInputStream 工作方式相同。

<代码>符号> (BufferedReader)或由他人(BufferedInputStream)表示,你希望能够退学。 如果你读得太多数据,那么标记可以是“无效的”,而打<代码>reset(将例外地失效。

A little example:

BufferedReader r = new BufferedReader(new StringReader(
    "Happy Birthday to You!
" +
    "Happy Birthday, dear " + System.getProperty("user.name") + "!"));
r.mark(1000); // save the data we are about to read
System.out.println(r.readLine()); // read the first line
r.reset(); // jump back to the marked position
r.mark(1000); // start saving the data again
System.out.println(r.readLine()); // read the first line again
System.out.println(r.readLine()); // read the second line
r.reset(); // jump back to the marked position
System.out.println(r.readLine()); // read the first line one final time

在这种例子中,我把<代码>StringReader列入一个<代码>BufferedReader,以获得<代码>readLine()方法,但<代码>StringReader已获得支持的<代码>>>>> /编号>和reset自有! 取自in-memory 数据来源的精度通常支持markreset<>t/code>本身,因为它们已经拥有所有数据,因此很容易再读。 从档案或管道或网络袖珍中读出的镜头并不自然地支持<条码><>t>>和<条码>reset,但你总是可以通过在<条码>中填入这一特征而添加这一特征。 BufferedInputStream或BufferedReader

http://www.un.org/Depts/DGACM/index_french.htm 这些方法提供了一个<代码>代码-编号的特征,使您得以在下游上阅读,以检查即将到来的数据。

http://ulibgcj.sourceforge.net/javadoc/io/BufferedReader.html“rel=“noreferer”>documentation:

The mark() method mark a position in the input to which the stream can be "reset" by calling the reset() method. The parameter readLimit is the number of chars that can be read from the stream after setting the mark before the mark becomes invalid. For example, if mark() is called with a read limit of 10, then when 11 chars of data are read from the stream before the reset() method is called, then the mark is invalid and the stream object instance is not required to remember the mark. Note that the number of chars that can be remembered by this method can be greater than the size of the internal read buffer. It is also not dependent on the subordinate stream supporting mark/reset functionality.

Reader:mark(int 改为Limit) 文件说:

Sets a mark position in this reader. The parameter readLimit indicates how many characters can be read before the mark is invalidated. Calling reset() will reposition the reader back to the marked position if readLimit has not been surpassed.

例:

import java.io.*;
import static java.lang.System.out;

public class App {

    public static final String TEST_STR = "Line 1
Line 2
Line 3
Line 4
";

    public static void main(String[] args) {

        try (BufferedReader in = new BufferedReader(new StringReader(TEST_STR))) {

            // first check if this Reader support mark operation
            if (in.markSupported()) {

                out.println(in.readLine());
                in.mark(0);                     // mark  Line 2 
                out.println(in.readLine());
                out.println(in.readLine());
                in.reset();                     // reset  Line 2 
                out.println(in.readLine());
                in.reset();                     // reset  Line 2 
                out.println(in.readLine());
                in.mark(0);                     // mark  Line 3 
                out.println(in.readLine());
                in.reset();                     // reset  Line 3 
                out.println(in.readLine());
                out.println(in.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

产出:

Line 1
Line 2
Line 3
Line 2
Line 2
Line 3
Line 3
Line 4

阅读界面可以读到而不是<>。 另一方面,变形的Reader则造成缓冲,因此,在阅读时,你能够返回一旁。 这就是这些方法。

用标记方法,你将“标记”放在一个位置上,然后可以读到。 一旦你们认识到,你就希望恢复你在这方面使用的重心。 从这一点来看,你再次读到同样的价值观。 你可以把它用于你们想要的东西。

如果你在表4中标出的5个果园,那么你将重塑你们的布图拉,那么,在BufferReader=“123456789”中,你就会发现以下果园。

这里就是一个例子。

int bufferSize = 4;
int readLimit = 4
ByteArrayInputStream byteInputStream = new ByteArrayInputStream("123456789abcdef".getBytes());
try(BufferedInputStream bufferedInputStream = new BufferedInputStream(byteInputStream, bufferSize)) {
        bufferedInputStream.mark(readLimit);
        System.out.print((char) bufferedInputStream.read());//byte1
        System.out.print((char) bufferedInputStream.read());//byte2
        System.out.print((char) bufferedInputStream.read());//byte3
        System.out.print((char) bufferedInputStream.read());//byte4
        bufferedInputStream.reset();
        System.out.print((char) bufferedInputStream.read());//byte5
        // Using this next reset() instead of the first one will throw an exception
        // bufferedInputStream.reset();

        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
        System.out.print((char) bufferedInputStream.read());
    }

产出:12341234

readLimit , here s a nice reference。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签