在Java中从URL读取/写入内容的首选方法是什么

jW.*_*jW. 4 java url

打开与网站的连接然后随后阅读该页面上的信息的首选方法是什么?关于不同部分似乎有许多具体问题,但没有明确和简单的例子.

aio*_*obe 5

从URL获取文本| 示例库:

try {
    // Create a URL for the desired page
    URL url = new URL("http://hostname:80/index.html");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Run Code Online (Sandbox Code Playgroud)

当谈到"写"到URL时,我想你会想要像使用URL发送POST请求一样 示例库.