0
try
        {
            URL url = new URL("http://localhost:8080/Files/textfile.txt");

            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);
            OutputStream outStream = connection.getOutputStream();
            ObjectOutputStream objectStream = new ObjectOutputStream(outStream);
            objectStream.writeInt(637);
            objectStream.writeObject("Hello there");
            objectStream.writeObject(new Date());
            objectStream.flush();
            objectStream.close();
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }

i am unable to write text into the file(textfile.txt) . i dn't know wat the       problem is??  can anyone explain how to write data to a text file based on url information ...  
4

2 回答 2

1

您需要在本地写入文件(下载后),然后再次通过 FTP 上传。或者,如果它位于您的服务器上,您需要将其作为File对象打开,然后使用BufferedWriter例如写入/附加到它。

try {
    BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
    out.write("aString");
    out.close();
} catch (IOException e) {
    // Handle exception
}

您需要从服务器的角度使用绝对/相对路径来定位文件以便写入它!

编辑:您可以在此处阅读有关 java 远程文件访问的更多信息。

于 2011-01-20T10:44:20.290 回答
0

永远不要使用类似的东西

System.out.println(e.toString());

这样您就可以释放堆栈跟踪,并且输出会转到通常应该转到 stderr 的 stdout。采用

e.printStackTrace();

反而。顺便说一句,在大型程序中不必要地捕捉异常是一个大问题,谷歌出“吞下异常”以了解更多信息。

于 2011-01-20T11:01:48.577 回答