把http://www.pepsi.com 的主页放在stuff目录中(在与GetURL应用程序相同的目录中),程序使用输入数据流下载数据,FileOutputStream数据流把内容显示到本地文件上。
编译通过,运行时总是会抛出MalformedURLException 异常
以下是全部程序段import java.net.*;
import java.io.*;public class GetURL {
static final int BUF_SIZE=4000;

public static void main (String[] args) {
int idx,tail,numb,dir_idx=0;
boolean noisy=false;
byte buffer[]=new byte [BUF_SIZE];
String outfile;
URL url;

InputStream in;
FileOutputStream out;

//Get command-line options
while(dir_idx<args.length&&args[dir_idx].startsWith("-")){
if(args[0].equals("-noisy")){
noisy=true;
}
else{
System.err.println("Unknown option: "+args[0]);
return ;
}
dir_idx++;
}//end of while loop

//Check for correct number of command-line arguments
if(args.length<dir_idx+2){
System.err.println("Usage: java GetURL [-noisy] "+"directory URL [URL...] ");
System.err.println("           copies the files specified    "+"by the URLs into directory ");
return ;
}
//Get the files
for(idx=dir_idx;idx<args.length;idx++){
//Open the URL
try{
url=new URL(args[idx]);
}catch(MalformedURLException e){
System.err.println("Error: malformed URL "+args[idx]);
System.err.println(e.getMessage());
continue;
}
try{
in=url.openStream();
}
catch(IOException e){
System.err.println("Error: could't access "+args[idx]);
System.err.println(e.getMessage());
continue;
}

//Extract file name and open file
tail=args[idx].lastIndexOf('\\');
if(tail==-1){
tail=args[idx].lastIndexOf('/');
}
outfile=args[idx].substring(tail+1);
try{
out=new FileOutputStream(args[dir_idx]+'/'+outfile);
}
catch(IOException e){
System.err.println("Error: couldn't create "+args[dir_idx]+'/'+outfile);
System.err.println(e.getMessage());
continue;
}
//Get the file
if(noisy){
System.out.println("Copyint "+outfile+"...");
}
try{
while((numb=in.read(buffer))!=-1)
out.write(buffer,0,numb);
}
catch(IOException e){
System.err.println("Error:IOException during copy ");
System.err.println(e.getMessage());
continue;
}
//Cleanup
try{
in.close();
}
catch(IOException e){
}

try{
out.close();
}
catch(IOException e){
}
}//end of for loop loading for each stream

if(noisy){
System.out.println("Done");
}
    }
    
    
}求高手指点!!