Heim  >  Fragen und Antworten  >  Hauptteil

node.js - Wie versteht man den Eingabe- und Ausgabestream von Java?

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();
      }
   }
}

Nach diesem Code zu urteilen, wird eine URL angefordert und der Inhalt gelesen und angezeigt. Warum wird hier jedoch getInputStream verwendet, sollte er nicht von getOutStream ausgegeben werden?

黄舟黄舟2683 Tage vor688

Antworte allen(1)Ich werde antworten

  • 我想大声告诉你

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

    InputStream 是用来读取的,OutputStream 是用来写入的;换句话说,输入流是指输入到系统中的流,系统从这个流中读取内容;输出流是指从系统输出的流,系统往这个流中写入内容。这个取名方式是站在使用者的角度,而不是 Stream 对象的角度。用过几次就习惯了。

    Antwort
    0
  • StornierenAntwort