简体   繁体   English

如何在Java中读取来自文本文件的指定字符

[英]How can specified chars from a text file be read in java

Given I have a text file, I know I can use FileReader to read chars : 给定我有一个文本文件,我知道我可以使用FileReader来读取chars

in = new FileReader("myFile.txt");
int c;
while ((c = in.read()) != -1)
{ ... }

However, after I do in.read() , would it be possible to backtrack by one character? 但是,在执行in.read() ,是否可以回溯一个字符? Is there any way I can change where in.read() is pointing to? 有什么方法可以更改in.read()指向的位置吗? Perhaps I could use an iterator? 也许我可以使用迭代器?

If you only need to backtrack by one character, consider keeping the previous character in a variable and then referencing that when needed. 如果只需要回溯一个字符,请考虑将前一个字符保留在变量中,然后在需要时引用它。

If you need to backtrack an unspecified amount, It's probably easier for most files to just save the file contents in memory and process the content there. 如果您需要回溯未指定的数量,则对于大多数文件而言,将文件内容保存在内存中并在那里进行处理可能会更容易。

The correct answer depends on context. 正确答案取决于上下文。

Assuming you are talking about an input Stream. 假设您正在谈论输入流。 You can use the int java.io.InputStream.read(byte[] b, int off, int len) method instead, the second parameter "off" (for offset) can be used as the starting point of the inputStream you want to read. 您可以使用int java.io.InputStream.read(byte [] b,int off,int len)方法代替,第二个参数“ off”(用于偏移量)可以用作您想要的inputStream的起点读。

An alternative is to use in.reset() first to re-position the reader to the start of the stream, and then in.skip(long n) to move to the desired position 另一种选择是使用in.reset()首先将阅读器重新定位到流的开头,然后使用in.skip(long n)移至所需位置

Depending on what you want to achieve you might have a look at PushbackInputStream or RandomAccessFile . 根据您要实现的目标,您可以查看PushbackInputStreamRandomAccessFile

Find below two snippets to demonstrate the different behaviors. 在下面找到两个片段,以演示不同的行为。 For both the file abc.txt contained a line foobar12345 . 对于这两个文件abc.txt包含一行foobar12345

The PushbackInputStream allows you to alter data in a stream to be read later. PushbackInputStream允许您更改流中的数据以供以后读取。

try (PushbackInputStream is = new PushbackInputStream(
        new FileInputStream("abc.txt"))) {
    // for demonstration we read only six values from the file
    for (int i = 0; i < 6; i++) {
        // read the next byte value from the stream
        int c = is.read();
        // this is only for visualising the behavior
        System.out.print((char) c);
        // if the current read value equals to character 'b'
        // we push back into the stream a 'B', which
        // would be read in the next iteration
        if (c == 'b') {
            is.unread((byte) 'B');
        }
    }
}

outout 外出

foobBa

The RandomAccessFile allows you to read a value on a specific offset in the stream. RandomAccessFile允许您读取流中特定偏移量的值。

try (RandomAccessFile ra = new RandomAccessFile("abc.txt", "r")) {
    // for demonstration we read only six values from the file
    for (int i = 0; i < 6; i++) {
        // read the next byte value from the stream
        int c = ra.read();
        // this is only for visualising the behavior
        System.out.print((char) c);
        // if the current read value equals to character 'b'
        // we move the file-pointer to offset 6, from which
        // the next character would be read
        if (c == 'b') {
            ra.seek(6);
        }
    }
}

output 输出

foob12

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

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