Java 在网站上读/写文本文件
基本上我已经将一个文本文件上传到我的主机,我想编辑该文件并用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不能像在本地文件系统上那样直接读/写网络服务器上的文件。您可能需要做的是:
,自动重新上传文件您可以在编辑器中完成这一切,并通过让它执行下载来将其隐藏在应用程序中-在后台编辑-保存-上传。许多文本编辑器将通过类似地建立远程连接并使文件写入往返对用户透明来实现此目的。
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:
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.
您应该实现某种远程过程调用。基本上,从客户端向服务器发送一条消息,其中包含您想要放入文件中的内容。然后让服务器实际打开该文件并将消息内容写入该文件。
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.