日期:2014-05-20  浏览次数:20956 次

URLConnection类向本地文件中写数据的问题
URLConnection类可以获得输出流,所以就想试试向本地文件中写数据,
程序如下:

import java.net.*;
import java.io.*;
public class URLConnectionTest
{

public static void main(String args[])
{
try
{

URL url=new URL("file:\\E:\\document\\hello.txt");
URLConnection con=url.openConnection();//返回一个URLConnection对象
con.setDoOutput(true);

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
bw.write("asdfadfadfasdfasdf");
bw.newLine();
bw.write("adfasdfasfadfsdf");
bw.newLine();
bw.close();
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
 

}

}
程序抛出以下异常:
 java.net.UnknownServiceException: protocol doesn't support output
  at java.net.URLConnection.getOutputStream(URLConnection.java:792)
  at URLConnectionTest.main(URLConnectionTest.java:18)

个人觉得是此句代码的问题:URL url=new URL("file:\\E:\\document\\hello.txt");
请高手予以解答,如何通过URL、URLConnection进行文件的写操作??
谢谢了

------解决方案--------------------
Java code

long begintime = System.currentTimeMillis();
          
           URL url = new URL("http://www.yhfund.com.cn");
           HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
           urlcon.connect();         //获取连接
           InputStream is = urlcon.getInputStream();
           BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
           StringBuffer bs = new StringBuffer();
           String l = null;
           while((l=buffer.readLine())!=null){
               bs.append(l).append("/n");
           }
           System.out.println(bs.toString());
          
           //System.out.println(" content-encode:"+urlcon.getContentEncoding());
           //System.out.println(" content-length:"+urlcon.getContentLength());
           //System.out.println(" content-type:"+urlcon.getContentType());
           //System.out.println(" date:"+urlcon.getDate());
                
           System.out.println("总共执行时间为:"+(System.currentTimeMillis()-begintime)+"毫秒");
        }catch(IOException e){
           System.out.println(e);
       }
    }
}









// 现在通过输出流对象构建对象输出流对象,以实现输出可序列化的对象。 
ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm); 

// 向对象输出流写出数据,这些数据将存到内存缓冲区中 
objOutputStrm.writeObject(new String("我是测试数据")); 

// 刷新对象输出流,将任何字节都写入潜在的流中(些处为ObjectOutputStream) 
objOutputStm.flush(); 

// 关闭流对象。此时,不能再向对象输出流写入任何数据,先前写入的数据存在于内存缓冲区中, 
// 在调用下边的getInputStream()函数时才把准备好的http请求正式发送到服务器 
objOutputStm.close(); 

// 调用HttpURLConnection连接对象的getInputStream()函数, 
// 将内存缓冲区中封装好的完整的HTTP请求电文发送到服务端。 
InputStream inStrm = httpConn.getInputStream(); // <===注意,实际发送请求的代码段就在这里 

// 上边的httpConn.getInputStream()方法已调用,本次HTTP请求已结束,下边向对象输出流的输出已无意义, 
// 既使对象输出流没有调用close()方法,下边的操作也不会向对象输出流写入任何数据. 
// 因此,要重新发送数据时需要重新创建连接、重新设参数、重新创建流对象、重新写数据、 
// 重新发送数据(至于是否不用重新这些操作需要再研究) 
objOutputStm.writeObject(new String("")); 
httpConn.getInputStream();

------解决方案--------------------
URL url=new URL("file:\\E:\\document\\hello.txt");

url是: file:\\ 不支持getOutputStream 
http:\\ 才可以
------解决方案--------------------
用文件的URL写没试过。不过看出错信息,难道用文
件URL不支持写吗?
------解决方案--------------------
探讨
程序抛出以下异常:
java.net.UnknownServiceException: protocol doesn't support output
at java.net.URLConnection.getOutputStream(URLConnection.java:792)