search

Home  >  Q&A  >  body text

node.js - How to understand Java's input stream and output stream?

import java.net.*;
import java.io.*;
public class URLConnDemo
{
   public static void main(String [] args)
   {
      try
      {
         URL url = new URL("http://www.xxx.com");
         URLConnection urlConnection = url.openConnection();
         HttpURLConnection connection = null;
         if(urlConnection instanceof HttpURLConnection)
         {
            connection = (HttpURLConnection) urlConnection;
         }
         else
         {
            System.out.println("请输入 URL 地址");
            return;
         }
         BufferedReader in = new BufferedReader(
         new InputStreamReader(connection.getInputStream()));
         String urlString = "";
         String current;
         while((current = in.readLine()) != null)
         {
            urlString += current;
         }
         System.out.println(urlString);
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Judging from this code, a URL is requested and the content is read and displayed, but why is getInputStream used here? Shouldn't it be output by getOutStream?

黄舟黄舟2747 days ago738

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-15 09:23:34

    InputStream is used for reading, and OutputStream is used for writing; in other words, the input stream refers to the stream input to the system, and the system reads content from this stream; the output stream refers to the stream output from the system , the system writes content into this stream. This naming method is from the user's perspective, not the Stream object's perspective. After using it a few times I got used to it.

    reply
    0
  • Cancelreply