//byte[] buffer=new byte[3*1024];
//int byte_read;
int c;
while((c=in.read())!=-1)
out.write((char)c);读取一网页的内容,被写入一文件
用BYTE读取,无显示
改用CHAR后方可,为啥?
//int byte_read;
int c;
while((c=in.read())!=-1)
out.write((char)c);读取一网页的内容,被写入一文件
用BYTE读取,无显示
改用CHAR后方可,为啥?
in=hp.openStream();
out呢?什么东西?应该可以的,你写完毕后out.flush()一下,再把out给关掉
import java.net.*;
class Geturl
{
public static void main(String [] args)
{
InputStream in=null;
OutputStream out=null;
try
{
if((args.length!=1)&&(args.length!=2))
throw new IllegalArgumentException("WRONG NUMBER OF ARGS");
URL hp=new URL(args[0]);
in=hp.openStream();
/*打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。此方法是下面方法的缩写:
openConnection().getInputStream()*/if(args.length==2)// 向指定的文件写内容
out=new FileOutputStream (args[1]);
else
out=System.out;//否则,向控制台输出//=======进行读取======
//byte[] buffer=new byte[3*1024];
//int byte_read;
int c;
while((c=in.read())!=-1)
out.write((char)c);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
try
{
in.close();
out.close();//此处需用到try-catch
}
catch(Exception e)
{}
}
}
}开始时,我用byte[]读取,没有任何内容;
改用 int c (char) c 之后才显示内容,为啥?