Question

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?

Was it helpful?

Solution 2

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

OTHER TIPS

I think that what you searching for is the mark method, you can read about it (and see a usage example) here: 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top