简体   繁体   中英

How to use properly BufferedReader

I have two BufferedReader objects:

BufferedReader br = new BufferedReader(new FileReader("tekst.txt"));
BufferedReader pmc = new BufferedReader(new FileReader("tekst.txt"));

What I want to do is read char by char the entire file. I need two Readers because one of them (pmc) will get sometimes "one char to many". So let's say the Reader br read 4 characters, and pmc read 5 characters - after that I want to assign br to pmc, so that both of them looked like they read 4 characters. Thought that simple

pmc = br;

would work, but pmc is still one character too far. How can I do this?

我认为您要搜索的是mark方法,您可以在此处阅读它(并查看用法示例): http : //www.tutorialspoint.com/java/io/bufferedreader_mark.htm

Can you post more code because your question is rather weird. Btw, if you have an object objA and an object objB, assigning one to another will not do what you think.

Example with Strings (also Object):

String strA = "TestA";
String strB = "TestB";
strB = strA;
strA = "blabla";
System.out.println(strB);

This will not print TestA but it will print blabla because when you assign one object to another, the other object will point to the same value in the memory so 2 objects will have only 1 value. If one of them is changed, in the memory the value is changed, thus both objects get changed.

You could use a PushbackReader instead of switching between two readers if you need to undo reading a character. It has an unread method, which allows you to put back the read char. See http://docs.oracle.com/javase/7/docs/api/java/io/PushbackReader.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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