//用这个试试。
import java.io.*;
import java.net.*; public class showfile2 {
public showfile2(){
}
public static void main(String args[]){
InputStream filecon = null;
DataInputStream filedata = null;
String fileline;
String url = "http://www.csdn.net/expert/topic/143/143518.shtm";
URL fileur;
try {
fileur = new URL(url);
filecon = fileur.openStream();
filedata = new DataInputStream(filecon);
while ((fileline = filedata.readLine()) != null) {
byte b[]=fileline.getBytes("ISO8859_1");
System.out.println(new String(b,"GB2312"));
}
}
catch (IOException e) { System.out.println("Error in I/O:");
}
}
}
import java.io.*;
import java.net.*; public class showfile2 {
public showfile2(){
}
public static void main(String args[]){
InputStream filecon = null;
DataInputStream filedata = null;
String fileline;
String url = "http://www.csdn.net/expert/topic/143/143518.shtm";
URL fileur;
try {
fileur = new URL(url);
filecon = fileur.openStream();
filedata = new DataInputStream(filecon);
while ((fileline = filedata.readLine()) != null) {
byte b[]=fileline.getBytes("ISO8859_1");
System.out.println(new String(b,"GB2312"));
}
}
catch (IOException e) { System.out.println("Error in I/O:");
}
}
}
;
try {
fileur = new URL(url);
filecon = fileur.openStream();
filedata = new DataInputStream(filecon);
while ((fileline = filedata.readLine()) != null) {
byte b[]=fileline.getBytes("ISO8859_1");
System.out.println(new String(b,"GB2312"));
}
}
catch (IOException e) { System.out.println("Error in I/O:");
}
filecon = fileur.openStream(); 对URL对象打开一个连接,并且放回一个流以便读取该连接。
filedata = new DataInputStream(filecon); 创建一个数据输入流从上面的那个输入流读取数据。 while ((fileline = filedata.readLine()) != null) {
byte b[]=fileline.getBytes("ISO8859_1");
System.out.println(new String(b,"GB2312"));
}
一行行的读直到数据结束。结束时以null表示。
由于java的中文问题,每次读入的一行都必须按iso88951的格式送到一个byte数组中,然后:Allocates a new String containg characters constructed from an array of 8-bit integer values. Each character c in the resulting string is constructed from the corresponding component b in the byte array such that c == (char)(((hibyte & 0xff) << 8)
| (b & 0xff)) Parameters:
ascii - the bytes to be converted to characters
hibyte - the top 8 bits of each 16-bit Unicode character。具体的java中文处理和unicode问题可以在论坛搜一下。