I didn't like to remember a lot of code in my memory ... so ... sometime I forget it T_T.
Then, I'd like to have some place that I can store my code like this :)
...
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
ref: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
...
If you'd like to write a file together, use this way ...
...
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class URLReaderAndWriter {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
FileOutputStream fout = new FileOutputStream("test.txt");
String inputLine;
while ((inputLine = in.readLine()) != null) {
fout.write(inputLine.getBytes());
fout.write('\n'); // for new line if you want
}
fout.close();
in.close();
}
}
Wednesday, May 13, 2009
Reading and Writing from a URL
Labels:
code,
java,
url reader,
url writer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment