BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {
}
int result = in.read();
// String result1=in.readLine();
char[] buf = new char[50];
in.read(buf);
String b = new String(buf);
text.setText(b);
I sent the word "hello world" from the server but what I got back is "ello world" from the above code . It's missing the first letter h. I used read instead of readLine because readLine doesn't work, it crashed.
Another issue, hello world is displayed in 2 lines instead of one. layout for textview is wrap_content.
This line is consuming the first character:
int result=in.read();
Hence when you do this, buf will not contain it:
in.read(buf);
You can use the mark() and reset() functions on the buffered reader if you need to go back to the beginning. Or otherwise just comment out that line.
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.print("Received string: '");
String inputLine;
String b = "";
while ((inputLine = in.readLine()) != null)
{
b = inputLine;
System.out.println(b);
//or do whatever you want with b
}
With this you will also be able to read multiple lines (in case you reveived more than one)...
I used read instead of readLine because readLine doesn't work, it
crashed.
It should not crash...i suggest you should fix this first
Related
I am creating an app in which it will send some command to server and i want to get the output of that command on client (android).
Basically i am sending command "systeminfo" and the output is too big to handle, so is there any way to get that big output on android as text view or anything else?
Code is as below
Process process = Runtime.getRuntime().exec("systeminfo");
and for get the output i have used
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I am confused how to use it as too much of string. Any reference material would be appreciated.
I'd recommend using a StringBuilder to reconstruct each line from the BufferedReader
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n"); // Remove the \n if you don't want newlines
}
final String execOutput = builder.toString();
OK, I'm officially frustrated with file handling on the Android. I'm new to it (only a few days) so I might just be missing something obvious. I have successfully written a short file of multiple lines with CSV data on each line. That's not the problem; reading is. I started with this (leaving out the exception handling and parsing for clarity):
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
BufferedReader buffreader = new BufferedReader( isr );
String inline;
while( (inline = buffreader.readLine())!=null)
{
// parse CSV here
}
I got an immediate null when reading the line. So I worked back up the line and tried reading raw bytes using various examples in SO as a pattern. I won't reproduce all the fails, but things like this didn't work either:
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
BufferedReader buffreader = new BufferedReader( isr );
CharBuffer buff = CharBuffer.allocate(1024);
isr.read(buff);
String s = buff.toString();
Debugging showed that the character buffer and string had consumed characters, but they were blank. Well, I can truly go old-school (I programmed in C before it was a decade old) if necessary. The following worked:
FileInputStream in = context.openFileInput("foo.txt");
InputStreamReader isr = new InputStreamReader( in );
int c;
String foo = "";
while( (c = isr.read())>=0)
foo += (char)c;
"foo" managed to have the whole contents which I can split up and process, but I dislike that it's so coarse. Yes, it works, but I'd like to know why the others didn't. I tried to stick closely to the various examples in SO, but had no success.
Any ideas about what might be wrong with the first attempts?
do {
inline=bruffreader.readLine();
if(inline != null) {
//parse here
}
} while (inline != null);
worked for me, it didn't return null
I have my textfile stored in assets folder and my requirement is to show the contents in textview pointwise.I am able to access the contents if there is no space in text file which is stored in assets folder.If I puts the space in the text file then i am not able to get the contents after space.How to achieve this means to show the contents pointwise.
for example my textfile is as follows
a)Americab)Africac)India
I want output as
a) America
b) Africa
c) India
Here is my code to access the text file from assest folder which I am getting.
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
Since you need to read multiple lines, you need to loop through, till you reach the EOF. Try something like this:-
StringBuffer sb = new StringBuffer(0);
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
sb.append("\n");
}
String wholeText = sb.toString();
You're only reading one line, you need to use a while loop and continue to read each line until the end of the file
InputStream in = this.getAssets().open("detailtext.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null){
// do whatever with line
}
I am receiving huge JSON and and while I am reading the lines OutOfMemoryError appears.
Here is my first method that I am trying to parse the JSON.
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
String result = "";
while (true) {
String ss = reader.readLine();
if (ss == null) {
break;
}
result += ss;
}
And I've also tried this method.
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null)
{
sb.append(line);
}
In the both of the cases the OutOfMemoryErorr is appearing.
Mostly the error will occur due to heap size. You need to increase the size of the heap
To increase the heap size
For additional info on heap size in java visit here
The best solution I found was to raise the Heap of the Application.
I placed android:largeHeap="true" under the <application/> in the AndroidManifest.xml.
When you instantiate your BufferedReader, you pass in an int size of 8, meaning your buffer is 8 characters (I assume that's not what you want). If you pass no size, the default is 8192 characters.
Try this:
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
Or this (same effect):
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8192);
http://developer.android.com/reference/java/io/BufferedReader.html
I'm not completely sure that will fix the error you are seeing, but worth trying if you can't get it working.
My JSON code was waiting for status, which comes towards the end. So I modified the code to return earlier.
// try to get formattedAddress without reading the entire JSON
String formattedAddress;
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
formattedAddress = ((String) ((JSONObject) new JSONObject(
jsonResults.toString()).getJSONArray("results").get(0))
.get("formatted_address"));
if (formattedAddress != null) {
Log.i("Taxeeta", "Saved memory, returned early from json") ;
return formattedAddress;
}
}
JSONObject statusObj = new JSONObject(jsonResults.toString());
String status = (String) (statusObj.optString("status"));
if (status.toLowerCase().equals("ok")) {
formattedAddress = ((String) ((JSONObject) new JSONObject(
jsonResults.toString()).getJSONArray("results").get(0))
.get("formatted_address"));
if (formattedAddress != null) {
Log.w("Taxeeta", "Did not saved memory, returned late from json") ;
return formattedAddress;
}
}
As the title says, I'm looking for a short function that reads in a file if there is a particular string and, //do something if present. For example if in the test.txt file is present the string TestString.
Someone would be so kind to suggest me a method?
InputStream is = //--open an input stream from file--
BufferedReader rd = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line;
while ( (line = rd.readLine()) != null ){
if(line.matches(".*TestString.*")){ //--regex of what to search--
//--do something---
break; //--if not want to search further--
}
}