i'm writing a code that get a Json String from the google geocode api , i'm using a StringBuilder to build the String from the Input stream that i have , well the problem is when
the String that the StringBuilder Have is not the same that the Function StringBuilder.tostring(); returns ...
i have a code like this :
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.i("line",line);
}
this gonna show all the lines using Log.i("line",line); so well .. but when when i do :
sb.toString();
It only returns some of the String that i want ... any suggestions guys ?
Note : The Json Result of the API is so long . you can try with this one
https://maps.googleapis.com/maps/api/geocode/json?address=Hospital+Jijel
Writing to the log will truncate your string if it's too long, which in your case it probably is.
Write the contents of your StringBuilder to the screen or a file to see its full contents.
Related
for some reason, when I take a string from my server to my app and Log it, the string is shorter than it needs to be.
I thought that it is happening because of the length of the string, but the whole string is 113137 characters long (and the limit is 10^32 -1).
The length of the string that returns to me is something like 4000.
Code:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
Log.d("Base64", stringBuilder.toString());
There is limit on log message length
#define LOGGER_ENTRY_MAX_LEN (4*1024)
#define LOGGER_ENTRY_MAX_PAYLOAD (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))
Also look at following question to clarify things
Android - Set max length of logcat messages
I have a text file which consists of a string ,I am parsing the file for my further purpose ,I want to parse by adding a single quote to a character after a particular string ,How to do that??
Text file data:
{Name:{ID:12342,age:32},type:s},{Name:{ID:12345,age:42},type:t},{Name:{ID:12348,age:35},type:s},{Name:{ID:12349,age:55},type:t}
Here I want to add a single quote to character after type:''
Expected o/p:
{Name:{ID:12342,age:32},type:'s'},{Name:{ID:12345,age:42},type:'t'},{Name: {ID:12348,age:35},type:'s'},{Name:{ID:12349,age:55},type:'t'}
My java code:
BufferedReader br = new BufferedReader(new FileReader("D:/Workspace/JAVA/Sample/EMP.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String value = sb.toString();
You could use the below string.replceAll function.
string.replaceAll("(?<=:)([a-zA-Z]+)", "'$1'");
This would add single quotes around the word(only letters) which exists next to the colon.
DEMO
(?<=type:)([^,}]*)
Try this.Replace by '$1'.See demo.
https://regex101.com/r/sJ9gM7/89
I'm developing an app. That app needs to get the content of a simple .php URL, and save it as a String.
The problem is that it is a very long String (VERY LONG) and it get's but in half. Take this link as an example:
http://thuum.org/download-dev-notes-web.php
With this code
URL notes = new URL("http://thuum.org/download-dev-notes-web.php")
BufferedReader in = new BufferedReader(new InputStreamReader(notes.openStream()));
String t = "";
while ((inputLine = in.readLine()) != null)
t = inputLine;
fOut = openFileOutput("notes", MODE_PRIVATE);
fOut.write(t.getBytes());
// Added This \/ to see it's length when divided, and it is not nearly as much as it should be
System.out.println(t.split("\\#").length);
Can someone tell me how would I be able to download that into a String, and save it into the internal storage without it getting cut? Some why it looks like it gets only the last x digits...
it seems you're overwriting your String t in every iteration of the while-loop. Try this:
StringBuilder result = new StringBuilder();
String inputLine = "";
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
fOut = openFileOutput("notes", MODE_PRIVATE);
fOut.write(result.toString().getBytes());
It creates a mutable StringBuilder and uses the resulting (immutable) String in the write call.
edit: I also recommend to always use curly brackets to indicate end of loop bodies, ommiting those can quickly lead to bugs, just check #gotofail for a recent example ;-)
My program needs to read from a multi-lined .ini file, I've got it to the point it reads every line that start with a # and prints it. But i only want to to record the value after the = sign. here's what the file should look like:
#music=true
#Volume=100
#Full-Screen=false
#Update=true
this is what i want it to print:
true
100
false
true
this is my code i'm currently using:
#SuppressWarnings("resource")
public void getSettings() {
try {
BufferedReader br = new BufferedReader(new FileReader(new File("FileIO Plug-Ins/Game/game.ini")));
String input = "";
String output = "";
while ((input = br.readLine()) != null) {
String temp = input.trim();
temp = temp.replaceAll("#", "");
temp = temp.replaceAll("[*=]", "");
output += temp + "\n";
}
System.out.println(output);
}catch (IOException ex) {}
}
I'm not sure if replaceAll("[*=]", ""); truly means anything at all or if it's just searching for all for of those chars. Any help is appreciated!
Try following:
if (temp.startsWith("#")){
String[] splitted = temp.split("=");
output += splitted[1] + "\n";
}
Explanation:
To process lines only starting with desired character use String#startsWith method. When you have string to extract values from, String#split will split given text with character you give as method argument. So in your case, text before = character will be in array at position 0, text you want to print will be at position 1.
Also note, that if your file contains many lines starting with #, it should be wise not to concatenate strings together, but use StringBuilder / StringBuffer to add strings together.
Hope it helps.
Better use a StringBuffer instead of using += with a String as shown below. Also, avoid declaring variables inside loop. Please see how I've done it outside the loop. It's the best practice as far as I know.
StringBuffer outputBuffer = new StringBuffer();
String[] fields;
String temp;
while((input = br.readLine()) != null)
{
temp = input.trim();
if(temp.startsWith("#"))
{
fields = temp.split("=");
outputBuffer.append(fields[1] + "\n");
}
}
I need to read a set of xml and property files and parse the data. Currently I am using inputstream ans string builder to do this. But this does not create the file in the same way as input file is. I donot want to remove the white spaces and new lines. How do i achieve this.
is = test.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String line5;
StringBuilder sb5 = new StringBuilder();
while ((line5 = br.readLine()) != null) {
sb5.append(line5);
}
String s = sb5.toString();
My output is:
#test 123 #test2 345
Expected output is:
#test
123
#test2
345
Any thoughts ? Thanks
br.readLine() consumes the line breaks, you need to add them to your StringBuilder after appending the line.
is = test.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String line5;
StringBuilder sb5 = new StringBuilder();
while ((line5 = br.readLine()) != null) {
sb5.append(line5);
sb5.append("\n");
}
If you want an extremely simple solution for reading a file to a String, Apache Commons-IO has a method for performing such a task (org.apache.commons.io.FileUtils).
FileUtils.readFileToString(File file, String encoding);
readLine() method doesn't add the EOL character (\n). So while appending the string to the builder, you need to add the EOL char, like sb5.append(line5+"\n");
The various readLine methods discard the newline from the input.
From the BufferedReader docs:
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
A solution may be as simple as adding back a newline to your StringBuilder for every readLine: sb5.append(line5 + "\n");.
A better alternative is to read into an intermediate buffer first, using the read method, supplying your own char[]. You can still use StringBuilder.append, and get a String will match the file contents.