Read large file data using buffered reader in android - java

hi i want to read large remote file into string using buffered reader.but i got half data of remote file.
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputstream),8*1024);
StringBuilder sb = new StringBuilder(999999);
String line;
while ((line = reader.readLine()) != null) {
Log.e("Line is ",line);
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("Content: ", sb.toString());
How to get full data of remote file?

It seems there is nothing wrong with your code but you can try it this way:
private String ReceiveData(InputStream inputstream){
StringBuilder sb = null;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( inputstream),8*1024);
sb = new StringBuilder();
String str;
int numRead = 0;
try {
if (bufferedReader!=null) {
if (bufferedReader.ready()) {
try {
while ((numRead = bufferedReader.read()) >= 0) {
//convert asci to char and then to string
str = String.valueOf((char) numRead);
if ((str != null)&& (str.toString() != "")) {
sb.append(str);
}
if (!bufferedReader.ready()){
//no more characters to read
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//loop exited, check for null
if (sb != null) {
return sb.toString();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
Hope I helped.

Related

How can i replace BufferedReader readline() by read()

I have this code :
try {
bufferedReader = new BufferedReader(new FileReader(new File(filePath)));
String currentLine;
int numLine = 0;
while ((currentLine = bufferedReader.readLine()) != null) {
numLine++;
for (String word : currentLine.split(" ")) {
if (!word.isEmpty() ) {
mapAllWordsPositionInFilesInFolder.computeIfAbsent(word,v -> new HashMap<>())
.computeIfAbsent(filePath, val -> new HashSet<>())
.add(numLine);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
I want change BufferedReader readline() by read() because it's more faster!
Someone can help please !

How do I display UTF-8 characters in google app engine's logs?

I'm currently using Java and I print my string using System.out.println(myString);
However, when I look at my server logs on the google app engine dashboard, I see a bunch of question marks (???) in place of where special characters (in my particular case, emoticons) would be.
The string is obtained directly from the payload of the request.
The payload of the request is being read as:
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String body = stringBuilder.toString();

how do i get a single line from a txt file on android studio

My Code to get file content:
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.text);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
but i want only one specific line on that file to be extracted.
Use BufferedReader instead of ByteArrayOutputStream.
String readLine(int line) throws IOException {
InputStream in = getResources().openRawResource(R.raw.text);
BufferedReader r = new BufferedReader(new InputStreamReader(in));
try {
String lineStr = null;
int currentLine = 0;
while ((lineStr = r.readLine()) != null) {
if (currentLine++ == line) {
return lineStr;
}
}
} finally {
if (r != null) {
r.close();
}
}
throw new IOException("line " + line + " not found");
}

Getting text from a website and putting it into one line?

How can i get all the text from a website (URL:http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698) and put it into a single line string, rather than more than one line, currently it will have around 20 lines give or take, but i want it to be in a single line.
Code for retreiving text:
public static void getTextFromURL() {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
Edit: You dont have to give me all the code, just a point in the right direction. :)
Easy, change the System.out.println to System.out.print. Done. :-D
To return a String instead, simply create a StringBuilder outside the loop, then append each line of the input to it.
Sample code to demonstrate the latter (I just realised what the OP wants, which is spaces instead of newlines):
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
if (result.length() != 0)
result.append(' ');
result.append(line);
}
return result.toString();
Hiren Patel's style of reading each character works too:
StringBuilder result = new StringBuilder();
while ((c = br.read()) != -1)
result.append(c == '\n' ? ' ' : (char) c);
return result.toString();
Retrieve the output instead of line to by character,
i.e. from while ((line = br.readLine()) != null) use while (int c != -1) read all characters and put them in a stringbuilder.
Then print the string at the end.
Edit:
use below code, it works:
public static void main(String args[]) {
URL url;
InputStream is = null;
try {
url = new URL("http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player=Hydro698");
is = url.openStream(); // throws an IOException
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = is.read();
while (ch != -1)
{
if((char)ch != '\n')
baos.write(ch);
ch = is.read();
}
byte[] data = baos.toByteArray();
String st = new String(data);
System.out.println(st);
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
Output is:
501844,110,34581332115,30,14114403982,1,18325274,31,15814460203,14,2405287276,11,1366419761,1,67679445,1,0505401,1,70522524,1,75454208,1,0505244,1,20505816,1,40469998,1,0337042,2,155393308,5,437403072,1,0488016,1,0524106,1,0428961,1,0389021,1,0382198,1,0383592,1,0362267,1,0-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1-1,-1
Hope when u run, u will understand that output is in a single line. String st = new String(data) is holding it.

very long string as a response of web service

I am getting a really long string as the response of the web service I am collecting it in the using the StringBuilder but I am unable to obtain the full value I also used StringBuffer but had no success.
Here is the code I am using:
private static String read(InputStream in ) throws IOException {
//StringBuilder sb = new StringBuilder(1000);
StringBuffer sb = new StringBuffer();
String s = "";
BufferedReader r = new BufferedReader(new InputStreamReader( in ), 1000);
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
s += line;
} in .close();
System.out.println("Response from Input Stream Reader >>>" + sb.toString());
System.out.println("Response from Input S >>>>>>>>>>>>" + s);
return sb.toString();
}
Any help is appreciated.
You can also split the string in array of strings in order to see all of them
String delimiter = "put a delimiter here e.g.: \n";
String[] datas=sb.toString().split(delimiter);
for(String string datas){
System.out.println("Response from Input S >>>>>>>>>>>>" + string);
}
The String may not print entirely to the console, but it is actually there. Save it to a file in order to see it.
I do not think that your input is too big for a String, but only not shown to the console because it doesn't accept too long lines. Anyways, here is the solution for a really huge input as characters:
private static String[] readHugeStream(InputStream in) throws IOException {
LinkedList<String> dataList = new LinkedList<>();
boolean finished = false;
//
BufferedReader r = new BufferedReader(new InputStreamReader(in), 0xFFFFFF);
String line = r.readLine();
while (!finished) {
int lengthRead = 0;
StringBuilder sb = new StringBuilder();
while (!finished) {
line = r.readLine();
if (line == null) {
finished = true;
} else {
lengthRead += line.length();
if (lengthRead == Integer.MAX_VALUE) {
break;
}
sb.append(line);
}
}
if (sb.length() != 0) {
dataList.add(sb.toString());
}
}
in.close();
String[] data = dataList.toArray(new String[]{});
///
return data;
}
public static void main(String[] args) {
try {
String[] data = readHugeStream(new FileInputStream("<big file>"));
} catch (IOException ex) {
Logger.getLogger(StackoverflowStringLong.class.getName()).log(Level.SEVERE, null, ex);
} catch (OutOfMemoryError ex) {
System.out.println("out of memory...");
}
}
System.out.println() does not print all the characters , it can display only limited number of characters in console. You can create a file in SD card and copy the string there as a text document to check your exact response.
try
{
File root = new File(Environment.getExternalStorageDirectory(), "Responsefromserver");
if (!root.exists())
{
root.mkdirs();
}
File gpxfile = new File(root, "response.txt");
FileWriter writer = new FileWriter(gpxfile);
writer.append(totalResponse);
writer.flush();
writer.close();
}
catch(IOException e)
{
System.out.println("Error:::::::::::::"+e.getMessage());
throw e;
}

Categories