i want to get the entire content of url (i.e) till the end of that url..
but after getting few contents because of partial loading ..i cant able to get remaining contents...is there is any way to get whole content from url even after the partial loading ...
String url = "URL/"; // getting URL
try {
Document doc = Jsoup.connect(url).get();
FileWriter fw=null;
BufferedWriter bw=null;
fw=new FileWriter("D:\\url.txt");
bw=new BufferedWriter(fw);
String line = doc.text();
System.out.println(line);
bw.write(line);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName())
.log(Level.SEVERE, null, ex);
}
Sorry for the late answer, dinner...
An answer can be found here: Read url to string in few lines of java code
Idk if it's your question is a duplicate question or not...
Anyways, the traditional way to do it would be like this:
URL website = new URL("example.com");
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
Hope I helped!
URL website;
try {
website = new URL("https://news.google.co.in/");
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
System.out.print(response.toString());
} catch (MalformedURLException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
and getting an html output
Related
I am trying to get data from URL and write it to a file, and I got the data from the URL but the file is empty, and below my code:
public class LiveRead {
public static void main(String[] args){
try {
URL u = new URL("http://quotes.rest/qod.json?category=life");
HttpURLConnection hr = (HttpURLConnection) u.openConnection();
if (hr.getResponseCode() == 200){
InputStream im = hr.getInputStream();
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(im));
FileOutputStream fo = new FileOutputStream("/Users/macos/Desktop/json");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));
String line = br.readLine();
while (line != null){
System.out.println(line);
bw.write(line);
bw.newLine();
line = br.readLine();
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
and when I open /Users/macos/Desktop/json, that file is empty. What's wrong and how to resolve it?
You should explicitly flush and close your writer, and not assume Java will do it for you when the program terminates:
bw.flush();
bw.close();
I am reading from a ".264" file using code below.
public static void main (String[] args) throws IOException
{
BufferedReader br = null;try {
String sCurrentLine;
br = new BufferedReader(new InputStreamReader(new FileInputStream("test.264"),"ISO-8859-1"));
StringBuffer stringBuffer = new StringBuffer();
while ((sCurrentLine = br.readLine()) != null) {
stringBuffer.append(sCurrentLine);
}
String tempdec = new String(asciiToHex(stringBuffer.toString()));
System.out.println(tempdec);
String asciiEquivalent = hexToASCII(tempdec);
BufferedWriter xx = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:/Users/Administrator/Desktop/yuvplayer-2.3/video dinalized/testret.264"),"ISO-8859-1"));
xx.write(asciiEquivalent);
xx.close();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Opening input and output file in HEX Editor show me some missing values, e.g. 0d (see pictures attached).
Any solution to fix this?
Lose InputStreamReader and BufferedReader, just use FileInputStream on its own.
No character encoding, no line endings, just bytes.
Its Javadoc page is here, that should be all you need.
Tip if you want to read the entire file at once: File.length(), as in
File file = new File("test.264");
byte[] buf = new byte[(int)file.length()];
// Use buf in InputStream.read()
I am trying to append to a text file but for some reason it keeps overwriting it, here's my code:
File logFile = new File(Environment.getExternalStorageDirectory().toString(), "notifications.txt");
try {
if(!logFile.exists()) {
logFile.createNewFile();
}
StringBuilder text = new StringBuilder(); // build the string
String line;
BufferedReader br = new BufferedReader(new FileReader(logFile)); //Buffered reader used to read the file
while ((line = br.readLine()) != null) { // if not empty continue
text.append(line);
text.append('\n');
}
BufferedWriter output = new BufferedWriter(new FileWriter(logFile));
output.write(text + "\n");
output.close();
alertDialog.show();
} catch (IOException ex) {
System.out.println(ex);
}
thank you in advance
use
new FileWriter(logFile, true)
Where the second parameter tells it to append, not overwrite.
Found in this question. In the related questions on the right.
Documentation can be found here
You need to use the other constructor of FileWriter that specifies whether the data is overwritten or appended. Use FileWriter(logFile, true) instead of what you have now :)
I've been trying to read a file for the last few days and have tried following other answers but have not succeeded. This is the code I currently have to import the text file:
public ArrayList<String> crteDict() {
try {
BufferedReader br = new BufferedReader
(new FileReader("/program/res/raw/levels.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] linewrds = line.split(" ");
words.add(linewrds[0].toLowerCase());
// process the line.
}
br.close();
}
catch (FileNotFoundException fe){
fe.printStackTrace();
It is meant to read the text file and just create a long Array of words. It keeps ending up in the FileNotFoundException.
Please let me know any answers.
Thanks!
IF your file is stored in the res/raw folder of the android project, you can read it as follows, this code must be inside an Activity class, as this.getResources() refers to Context.getResources():
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(R.raw.levels);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
My code is reading an HTML page from the web and I want to write good code, so I would like to close the resources using try-with-resources or finally block.
With the following code it seems impossible to use either of them to close "in".
try {
URL url = new URL("myurl");
BufferedReader in = new BufferedReader(
new InputStreamReader(
url.openStream()));
String line = "";
while((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (IOException e) {
throw new RuntimeException(e);
}
Would you be able to write the same code using try-with-resources or finally?
I don't see any particular difficulty with the following:
try (BufferedReader in = new BufferedReader(new InputStreamReader(
new URL("myurl").openStream()))) {
String line = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Is it not what you're looking for?