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 ...  
有帮助吗?

解决方案

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

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

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

编辑: 您可以在Java中阅读有关远程文件访问的更多信息 这里.

其他提示

永远不要使用类似的东西

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

这样,您就会松开堆栈跟踪,然后输出将其通常应转到STDERR。利用

e.printStackTrace();

反而。顺便说一句,在较大的程序中,到处都是一个很大的问题,Google出了“吞咽例外”以了解更多信息。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top