2

I've used readline() method to read first line of my buffer reader then,after checking something in the first line, I want to send my buffer reader into function, without loosing the first line, As I understood, the readline() method, seek the first line of buffer reader, how should I reverse it?

BufferedReader bufferedReader = new BufferedReader(serverinput);
String onelineofinput = bufferedReader.readLine();
if(onelineofinput...)
...
  then
  sendpackets(IPaddress, port, bufferedReader, connection);
  //bufferedreader should send completely here.
Happy Developer
  • 617
  • 1
  • 8
  • 15
  • not quite a duplicate since the OP does not specifically ask about mark() and reset(). – wero Mar 03 '16 at 15:26

1 Answers1

1

You can create a java.io.PushbackReader between the BufferedReader and the input.

After you have examined the first line you push it back to the PushbackReader (and also the line break which is swallowed by the BufferedReader) and then pass the PushbackReader to sendpackets.

PushbackReader pbreader = new PushbackReader (serverinput);
BufferedReader bufferedReader = new BufferedReader(pbreader);
String onelineofinput = bufferedReader.readLine();
if (onelineofinput...) {
    pbreader.unread(onelineofinput.toCharArray());
    pbreader.unread('\n');
    sendpackets(IPaddress, port, new BufferedReader(pbreader), connection);
} 
wero
  • 32,544
  • 3
  • 59
  • 84