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

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.

Related

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();

Read large file data using buffered reader in android

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.

I am not able to write all data to a file

I have written the Java code to read from one file and write to a new file. The file from which I am reading has 5000 lines of records, but when I am writing to a new file I am able to write only between 4700-4900 records.
I think may be I am simultaneously reading from a file and writing to a file, which might be creating a problem.
My code is as follows:
Reading from a file:
public String readFile(){
String fileName = "/home/anand/Desktop/index.txt";
FileReader file = null;
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null) {
line.replaceAll("ids", "");
System.out.println(line);
returnValue += line + "\n";
}
return returnValue;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
// Ignore issues during closing
}
}
}
}
Writing to a file:
public void writeFile(String returnValue){
String newreturnValue = returnValue.replaceAll("[^0-9,]", "");
String delimiter = ",";
String newtext ="";
String[] temp;
temp = newreturnValue.split(delimiter);
FileWriter output = null;
try {
output = new FileWriter("/home/anand/Desktop/newinput.txt");
BufferedWriter writer = new BufferedWriter(output);
for(int i =0; i < temp.length ; i++){
writer.write("["+i+"] "+temp[i]);
writer.newLine();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
// Ignore issues during closing
}
}
}
}
I need the suggestion to how to simultaneously read and write to a file.
You need to close writer instead of output. The BufferedWriter may not be writing all of the lines, and won't since you never close it.
You have to close the writer object. The last couple lines probably haven't been flushed onto the text file.
In addition, are you aware of the try-with-resource introduced in Java 7? You can condense your code to this by utilizing it:
public String readFile(){
String fileName = "/home/anand/Desktop/index.txt";
try(BufferedReader reader = new BufferedReader(new FileReader(filename)) {
String line = "";
while ((line = reader.readLine()) != null) {
line.replaceAll("ids", "");
System.out.println(line);
returnValue += line + "\n";
}
return returnValue;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
By doing this, Java will automatically close the reader object for you once the try block completes, regardless of whether or not an exception was thrown. This makes it easier to read your code :)

New line after 12 chars java

I have a class which can read a file, modify it an write it to another file. The characters in the output are correct , the only problem is that the lines need to have a length of 12 chars.
How can I achieve this with my existing code?(I wrote a comment where in the code I want to do this)
My input file: http://gyazo.com/13fe791d24ef86e29ab6a6e89d0af609
The current output: http://gyazo.com/cc195c1d59a9d1fe3b4f2c54e71da8eb
The output I want : http://gyazo.com/04efcbb05c5d56b6e28972feb8c43fb8
String line;
StringBuilder buf = new StringBuilder();
public void readFile(){
BufferedReader reader = null;
try {
File file = new File("C:/Users/Sybren/Desktop/Invoertestbestand1.txt");
reader = new BufferedReader(new FileReader(file));
//String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
//buf.append(line);
processInput();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
};
}
}
public void processInput(){
buf.append(line);
if (buf.length()>7){
buf.append("-");
}
/* if a * is followed by * change them to a ! */
for (int index = 0; index < buf.length(); index++) {
if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
buf.setCharAt(index, '!');
buf.deleteCharAt(index+1);
}
}
// get last character from stringbuilder and delete
buf.deleteCharAt(buf.length()-1);
/* start with a new line if the line length is bigger than 12 - how to do it? */
//???
}
public void writeFile() {
try {
String content = buf.toString();
File file = new File("C:/Users/Sybren/Desktop/uitvoer1.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}}
Would something along these lines help?
for (int i=13;i<buf.size();i+=13) {
buf.insert(i, '\n');
i++; // to account for the newline char just added
}
The numbers used may not be correct, either because of misunderstanding of the question or because it isn't tested.
for (int index = 0; index < buf.length(); index++) {
if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
buf.setCharAt(index, '!');
buf.deleteCharAt(index+1);
}
}
There will be an java.lang.ArrayIndexOutOfBoundsException at the end of the loop when you you call index+1

Output only giving me one line

Can anyone point me in the right direction here. I have a method that is supposed to read a file and display the data in that file. I can only get it to display one line. I know it is something simple I am over looking, but my brain is mush and I just keep digging a bigger hole.
public static String readFile(String file) {
String data = "";
if (!new java.io.File(file).exists()) {
return data;
}
File f = new File(file);
FileInputStream fStream = null;
BufferedInputStream bStream = null;
BufferedReader bReader = null;
StringBuffer buff = new StringBuffer();
try {
fStream = new FileInputStream(f);
bStream = new BufferedInputStream(fStream);
bReader = new BufferedReader(new InputStreamReader(bStream));
String line = "";
while (bStream.available() != 0) {
line = bReader.readLine();
if (line.length() > 0) {
if (line.contains("<br/>")) {
line = line.replaceAll("<br/>", " ");
String tempLine = "";
while ((tempLine.trim().length() < 1)
&& bStream.available() != 0) {
tempLine = bReader.readLine();
}
line = line + tempLine;
}
buff.append(line + "\n");
}
}
fStream.close();
bStream.close();
bReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
String line = null;
while ((line = bReader.readLine())!=null)
How about doing this with Guava:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html
List<String> lines = Files.readLines("myFile.txt", Charset.forName("UTF-8"));
System.out.println(lines);
You'd still have to do a little bit of work to concatenate the <br> lines etc...

Categories