try { // Create a URL for the desired page URL url = new URL("http://www.google.com"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String text=""; String str; while ((str = in.readLine()) != null) { System.out.println(str); text+=str; // str is one line of text; readLine() strips the newline character(s) } in.close(); File file = new File("out.txt"); RandomAccessFile waf = new RandomAccessFile(file, "rw"); waf.writeBytes(text); waf.close(); } catch (MalformedURLException e) { System.out.println("MalformedURLException"); } catch (IOException e) { e.printStackTrace(); System.out.println("IOException"); }
import java.net.URL; import java.io.BufferedOutputStream; import java.io.BufferedInputStream; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.File;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class PictureThief { public PictureThief() { } public static void main(String[] args) { try { URL url = new URL( "http://www.donews.net/redtroy"); java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream()); byte[] bytes = new byte[100]; OutputStream bos = new FileOutputStream(new File("C:\\index_logo.gif")); int len; while ( (len = bis.read(bytes)) > 0) { bos.write(bytes, 0, len); } bis.close(); bos.flush(); bos.close(); } catch (Exception e) { e.printStackTrace(); } }}
OutputStream bos = new FileOutputStream(new File("C:\\index_logo.gif")); 改成 OutputStream bos = new FileOutputStream(new File("C:\\index.htm")); 或 OutputStream bos = new FileOutputStream(new File("C:\\index.txt"));
// Create a URL for the desired page
URL url = new URL("http://www.google.com");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String text="";
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
text+=str;
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
File file = new File("out.txt");
RandomAccessFile waf = new RandomAccessFile(file, "rw");
waf.writeBytes(text);
waf.close();
} catch (MalformedURLException e) {
System.out.println("MalformedURLException");
} catch (IOException e) {
e.printStackTrace();
System.out.println("IOException");
}
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class PictureThief {
public PictureThief() {
} public static void main(String[] args) {
try {
URL url = new URL(
"http://www.donews.net/redtroy");
java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
byte[] bytes = new byte[100];
OutputStream bos = new FileOutputStream(new File("C:\\index_logo.gif"));
int len;
while ( (len = bis.read(bytes)) > 0) {
bos.write(bytes, 0, len);
}
bis.close();
bos.flush();
bos.close(); }
catch (Exception e) {
e.printStackTrace();
}
}}
改成 OutputStream bos = new FileOutputStream(new File("C:\\index.htm"));
或 OutputStream bos = new FileOutputStream(new File("C:\\index.txt"));