node.js - How to understand Java's input stream and output stream?
黄舟
黄舟 2017-06-15 09:21:34
0
1
655
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?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
我想大声告诉你

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!