写入远程文件并将字符串转换为 url

发布于 2024-12-12 14:16:59 字数 222 浏览 0 评论 0原文

我想写入远程文件并读取远程文件的内容。我的字符串就像 http://www.mywebsite.info/other/poll.txt 。如何将此字符串转换为 URL/URI ?我的目标是写入和读取服务器上托管的文件的内容。 (通过小程序)

我可以使用 FileReaderFileWriter 来实现此目的吗?

I want to write to a remote file as well as read the contents of a remote file. My string is like http://www.mywebsite.info/other/poll.txt . How to convert this string into URL/URI ? My goal is to write as well as read the contents of the file hosted on my server. (via applet)

Can i use FileReader and FileWriter for this ?

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

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

发布评论

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

评论(2

舟遥客 2024-12-19 14:17:05

尝试这样的阅读方式 -

        try {
            URL url = new URL("http://www.mywebsite.info/other/poll.txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.close();
        } catch (MalformedURLException e) {

              // do something meaningful here when the exception is caught
        } catch (IOException e) {

             // do something meaningful here when the exception is caught
        }

Try something like this for reading -

        try {
            URL url = new URL("http://www.mywebsite.info/other/poll.txt");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }
            in.close();
        } catch (MalformedURLException e) {

              // do something meaningful here when the exception is caught
        } catch (IOException e) {

             // do something meaningful here when the exception is caught
        }
风蛊 2024-12-19 14:17:04

FileReader 和 FileWriter 用于从文件系统读取文件或向文件系统写入文件。

Applet 使用 HTTP 与其源服务器通信。 HTTP 并不是用于读写文件的协议。要读取它,您需要打开到此 URL 的 HttpUrlConnection。要编写它,您需要有一些服务器组件(PHP 应用程序、Servlet 等)并向该服务器组件发送适当的请求,以便它写入文件。

在尝试编写小程序之前,请先了解 HTTP 的工作原理。

FileReader and FileWriter are used to read and write files from/to the file system.

An applet communicates with its origin server using HTTP. And HTTP isn't a protocol used to read and write files. To read it, you need to open a HttpUrlConnection to this URL. To write it, you'll need to have some server component (a PHP application, a Servlet, whatever) and send an appropriate request to this server component so that it writes to the file.

Read up on how HTTP works before trying to write your applet.

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