Java 在网站上读/写文本文件

发布于 2024-12-10 04:20:23 字数 708 浏览 0 评论 0原文

基本上我已经将一个文本文件上传到我的主机,我想编辑该文件并用 java 读取它。我已经为其创建了权限,但我不知道如何使用 Java 来做到这一点。这是我在本地读取/写入的代码:

读取:

BufferedReader mainChat = new BufferedReader(new FileReader("./messages/messages.txt"));
String str;
    while ((str = mainChat.readLine()) != null) 
    {
        System.out.println(decrypt.Decrypt(str, salt));
    }
    mainChat.close();

写入:

    FileWriter chatBuffer = new FileWriter("./messages/messages.txt",true);
    BufferedWriter mainChat = new BufferedWriter(chatBuffer);
    mainChat.write(message);
    mainChat.newLine();
    mainChat.flush();
    mainChat.close();

我必须如何修改它才能使其工作?谢谢

Basically I've uploaded a text file to my host and I want to edit the file and read it with java. I've created the permissions for it but im not sure how to do it with Java. This is my code which read/writes locally:

Read:

BufferedReader mainChat = new BufferedReader(new FileReader("./messages/messages.txt"));
String str;
    while ((str = mainChat.readLine()) != null) 
    {
        System.out.println(decrypt.Decrypt(str, salt));
    }
    mainChat.close();

Write:

    FileWriter chatBuffer = new FileWriter("./messages/messages.txt",true);
    BufferedWriter mainChat = new BufferedWriter(chatBuffer);
    mainChat.write(message);
    mainChat.newLine();
    mainChat.flush();
    mainChat.close();

How would I have to modify this to make it work? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-12-17 04:20:23

我认为您不能像在本地文件系统上那样直接读/写网络服务器上的文件。您可能需要做的是:

  1. 下载文件
  2. 在本地编辑器中打开它
  3. 保存后

,自动重新上传文件您可以在编辑器中完成这一切,并通过让它执行下载来将其隐藏在应用程序中-在后台编辑-保存-上传。许多文本编辑器将通过类似地建立远程连接并使文件写入往返对用户透明来实现此目的。

I don't think you can read/write directly to a file on a web server the way you would on a local filesystem. What you'll probably need to do is:

  1. download the file
  2. open it in the local editor
  3. when it's saved, automatically re-upload the file

You can do this all within the editor, and hide this in the app by having it do the download-edit-save-upload in the background. Many text editors will do this by establishing a remote connection similarly, and making the file writing round trip transparent to user.

挥剑断情 2024-12-17 04:20:23

您应该实现某种远程过程调用。基本上,从客户端向服务器发送一条消息,其中包含您想要放入文件中的内容。然后让服务器实际打开该文件并将消息内容写入该文件。

You should implement some sort of remote procedure call. Basically, from the client send the server a message containing what you'd like to put in the file. Then have the server actually open the file and write the content of the message to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文