Java:在线保存到文件

标签 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();

最佳答案

您可以使用 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();

关于Java:在线保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33431798/

相关文章:

ios - 将 NSDictionary 写入 plist

Null 但既不是 Empty 也不是 Blank 的 Java 注释

java - XML 处理器支持模式 1.1

python - 将数据帧字典写入文件

ios - 创建单独的文件

iphone - NSData writeToFile 适用于模拟器但不适用于设备

java - 删除并重命名有问题的文件

java - recyclerview 列表不显示通过 arraylist 传递的数据

java - 如何通过将字符串作为对 Java 中数组的引用传递来返回数组?

c++ - 写入文件权限被拒绝