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
}
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();
In every new version of my app I like to put a What's New section which is basically a long(-ish) text that tends to have a lot of apostrophes and percentage signs and newlines and what have you.
Is there some easier way of loading it in a textview other than escaping all the problematic characters and replacing newlines with \n or <br />?
Why don't you store the text as a Raw text file in resource folder and read it
InputStream inputStream = getResources().openRawResource(R.raw.share);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = reader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
And then set to TextView
textView.setText(text.toString());
I'm trying to get the content Text of a Message in my Android App with an InputStream, because there I can get a line Separator. I'm getting the following Exception when I'm trying it:
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.InputStream
This is my Code:
Object o = message.getContent();
InputStream is = (InputStream)o;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
Do you know what the problem is? In every Javamail - Thread you can read that this Method runs.
Use the Message.getInputStream method.
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
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--
}
}