Java:在线保存到文件

时间:2015-10-30 08:43:35

标签: java writetofile

目前,我有这个:

String URL = "//Somewhere in my computer";
PrintWriter list = new PrintWriter(new FileWriter(URL, true));

是否可以写入包含文档的在线网站?例如,有些站点只需要在该文档中写入站点的URL。是否可以从java程序中读取和编辑?

这就是我为TitanPad服务所做的:

String someText = "Here goes magical text";

    URL url = null;
    try {
        url = new URL("https://titanpad.com/3VBeN3Xo31");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URLConnection connection = null;
    try {
        connection = url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }

    connection.setDoOutput(true);

    OutputStream outStream = null;
    try {
        outStream = connection.getOutputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        outStream.write(someText.getBytes(Charset.forName("UTF-8")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    try {
        outStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

1 个答案:

答案 0 :(得分:2)

您可以使用FTP -

String someText = "Here goes magical text";

URL url = new URL("ftp://user:password@somewebsite.com/filename.txt");
URLConnection connection = url.openConnection();

connection.setDoOutput(true);

OutputStream outStream = connection.getOutputStream();

outStream.write(someText.getBytes(Charset.forName("UTF-8"))); 
outStream.close();