read all lines from string buffered reader - java

I have simple client/server program that sends and recieves strings from client to server and vice versa.
Some string contain newline characters "n\", eg "ERR\nASCII: OK"
my buffered reader:
BufferedReader in =
new BufferedReader(
new InputStreamReader(ConverterSocket.getInputStream()));
I am trying to display each line in the string to the user/ client.
I have tried the following for loop:
for (line = in.readLine(); line != null; line = in.readLine()){
System.out.println(line);
}
output (as expected):
ERR
ASCII: OK
but the loop doesn't end. I have also tried:
while ((line = in.readLine()) != null){
system.out.println(line)
}
which also doesn't end properly.
How can i print all lines in the string?

The readLine() method returns null at end of stream, which doesn't occur until the peer closes the connection.

Related

BufferedReader keeps waiting for response

I have a server and client running on local server.
I read from the server this way:
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
public static String readResponse() throws IOException{
String response = "";
String line;
while((line = br.readLine()) != ""){
System.out.println("s: " + line);
}
return response;
}
And I get the response from server but the program stops and doesn't go anywhere from there:
Please enter option number: 1
c: MSGGET
s: 200 OK
s: Go for it now. The future is promised to no one.
And it just hangs here, when it is suppose to continue.
I also tried:
while((line = br.readLine()) != null){
It just keeps waiting. Is there anything maybe on the server that I have to change to tell the client that I am done transmitting data.
Please help! Thank you!
while((line = br.readLine()) != ""){
System.out.println("s: " + line);
}
return response;
}
Unless your peer is planning to transmit a blank line as an end-of-message sentinel, this loop is pointless, and it also compares Strings incorrectly.
And I get the response from server but the program stops and doesn't go anywhere from there.
It is waiting for an empty line that never arrives. And it is ignoring the end of stream condition.
while((line = br.readLine()) != null){
Now this loop is correct, but it won't exit until end of stream, which won't happen until the peer closes the connection.
It just keeps waiting.
That's what it's supposed to do.
Try this one :
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
public static String readResponse() throws IOException{
String response = "";
String line;
while((line = br.readLine()) != "" || (line = br.readLine()) != null)){
System.out.println("s: " + line);
}
return response;
}
if this is not working then you need to check if you are sending some special character on end of stream from server. Then apply check for that character.
I ended up using this:
while(!(line = br.readLine()).equals("exit")){
And I asked the person responsible for the server to print the following when he is done writing a response:
//this was initiated at the beginning of the program
out = new PrintWriter(socket.getOutputStream(), true);
out.println("exit");
This seems to works well for me so far!
Thank you everyone for your help!

Java - BufferedReader not reading characters

So below is my code. I am having it read from a csv file with values (each one on a newline)
54232
65
6564
6232
67413
26
completely meaningless but I'm calling a sysout after its read a line and it's returning
��5 followed by newlines
I can however use this arraylist to save the file and it saves it just as before except the first value has some Chinese characters strapped on to the start. I have absolutely no idea.
BufferedReader buffer = new BufferedReader(new FileReader(file));
ArrayList<String> lines = new ArrayList();
String line = "";
while ((line = buffer.readLine()) != null) {
System.out.println(line);
lines.add(line);
}
buffer.close();
return lines;
Solved
There was a BOM in my CSV.
Not used to LibreOffice. Fiddled about with the save settings then it worked just fine

getInputStream() is not returning readable information

My homework assignment is to call a .jar from within a java program but I can't get the input stream to return the results into something readable. Here is what I did first:
InputStream in = proc.getInputStream();
System.out.println(in);
But that didn't work, and I found some different variation:
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(input);
But that didn't work either, both scenarios it returned something like this: java.io.BufferedReader#2ce908. How can I get it to return a readable output?
EDIT: This is a java program receiving the output from another java program. The program that the user starts is called Translate.java which takes in English words and passes them as command line arguments using the Runtime.getRuntime().exec("java -jar Dictionary.jar"+) command. I was told to use the getInputStream() in Translate.java to receive the output from the Dictionary.java program.
A stream is not text; it is a thing that reads a file for you. You have to call readLine:
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = null;
while ((s = input.readLine()) != null) { // it returns null at the end of the file
System.out.println(s);
}

Looping over InputStream truncating data

So this is a very simple problem with a simple solution that I'm just not seeing:
I'm trying to get a list of data through an InputStream, looping until I reach the end of the stream. On each iteration, I print the next line of text being passed through the InputStream. I have it working but for one small problem: I'm truncating the first character of each line.
Here's the code:
while (dataInputStream.read() >= 0) {
System.out.printf("%s\n", dataInputReader.readLine());
}
And the output:
classpath
project
est.txt
Now, I know what's going on here: the read() call in my while loop is taking the first char on each line, so when the line gets passed into the loop, that char is missing. The problem is, I can't figure out how to set up a loop to prevent that.
I think I just need a new set of eyes on this.
readLine for DataInputStream is deprecated. You may try wrapping it with a BufferedReader:
try
{
String line;
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( dataInputStream ) );
while( (line = bufferedReader.readLine()) != null )
{
System.out.printf("%s\n", line);
}
}
catch( IOException e )
{
System.err.println( "Error: " + e );
}
Also, I`m not sure, that it is a good idea to use available() due to this specification:
* <p>Note that this method provides such a weak guarantee that it is not very useful in
* practice.
Use one BufferedReader and InputStreamReader, here is one example:
InputStream in=...;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (br.ready()) {
String line = br.readLine();
}
dataInputStream.read() reads the first character of the InputStream, the same as dataInputReader.readLine() reads the complete next line. Every read character or line is then gone. you can use the dataInputStream.available() to check if the InputStream has data available.
That should print the correct output:
while (dataInputStream.available()) {
System.out.printf("%s", dataInputReader.read());
}
String line;
while ((line = dataInputReader.readLine()) != null) {
System.out.println(line);
}

Could it be that line.containes() is missing a search-term in Android?

Something strange happens on a text parser that I am writing an I thought maybe you experts can find what I am doing wrong.
The parser is searching for several search-terms in the text and copy the found results to an output parsed file on the SD card.
The code is:
String line; // line reading buffer
…
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
…
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
/*** parsing the buffer line by line ***/
if (line.contains("SearchTermA")){
// parsing the text in this line
}
if (line.contains("SearchTermB")){
// parsing the text in this line
}
// Check that we got to a cretin part in the file
if (line.contains("SearchTerm_text")){
textID = 1;
}
if (textID == 1){
if (line.contains("SearchTermC")){
// parsing the text in this line
}
}
Now, the issue is that at the beginning of the file (the file is very long) this works OK but sometimes along the way SearchTermB appears in the original text but is not deceted by the code. I tried stepping the code with Eclipse on the target android machin and I can clearly see that “line” contains SerchTermB but the debugger ignores this IF statement and continue to the next IF statement.
Could it be that line.containes() is missing a search-term?
Please help in finding what am I doing wrong since this prevents me from sleeping at night…..
Thanks,
Your while loop is reading two lines at a time...
while ((line = bufferedReader.readLine()) != null){
line = bufferedReader.readLine(); // reading the buffer
In other words you call readLine() in your while statement in order to check that it's not a null result and then you immediately call readLine() again.
Get rid of this...
line = bufferedReader.readLine(); // reading the buffer
...and see what happens.

Categories