Having problems reading from a BufferedReader

I'm writing an FTP client, I'm using InputStreamReader then BufferedReader. I'm having trouble reading all the data. The best way seems to be the following.

i = br.read();

while(i != -1)

{

TextArea.append(br.readLine());

i = br.read();

}

The problem I have with this method is that I am missing the first character of each line.

The other method I'm trying is just to read the line then if the line contains a - after the FTP reply number then it loops until it gets to a line that contains a space. Some sites are different though so I'd like to use the first one.

Thanks,

Jason

[653 byte] By [CaffeinatedCoder] at [2007-9-30 19:06:35]
# 1

Hi ,

You have using i=br.read() inside loop as well as before while loop. which will return you int type.

But keep in mind that this is due to BufferedReader already reads one charecter so you dont get that char in br.readLine() as it is no more in buffer.

you can confirm this by adding followin line after each br.read();

System.out.println("Charecter Missing is "+(char)i)

softclimax at 2007-7-6 23:16:47 > top of Java-index,Administration Tools,Sun Connection...
# 2

I think I solved it, I used the mark and reset methods. Although I'm not sure it's a good idea to bounce around the data stream like that.

Heres the code

while(amount != -1)

br.mark(1); //Reads 1 character

amount = br.read(); //Returns the current value of the buffer, but loses that character

br.reset(); //Reclaims the character lost from the read

txtResult.append(br.readLine());

}

CaffeinatedCoder at 2007-7-6 23:16:47 > top of Java-index,Administration Tools,Sun Connection...
# 3

i = br.read();

while(i != -1)

{

TextArea.append(br.readLine());

i = br.read();

}

The reason it was advancing one character is because when you set i you were setting it to br.read(), which advances the reader one character .Set your counter to zero instead of br.read(). Example:

int i = 0;

while(i != -1)

{

TextArea.append(br.readLine());

i = br.read();

}

jamesstaylor at 2007-7-6 23:16:47 > top of Java-index,Administration Tools,Sun Connection...