node.js - 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(); } }}
從這段代碼來看,請求一個url并把內容讀取出來顯示,但是為什么這里用到getInputStream,應該不是getOutStream 輸出嗎?
問題解答
回答1:InputStream 是用來讀取的,OutputStream 是用來寫入的;換句話說,輸入流是指輸入到系統中的流,系統從這個流中讀取內容;輸出流是指從系統輸出的流,系統往這個流中寫入內容。這個取名方式是站在使用者的角度,而不是 Stream 對象的角度。用過幾次就習慣了。
相關文章:
1. 如何解決docker宿主機無法訪問容器中的服務?2. springboot - spring-boot-starter-thymeleaf對沒有結束符的HTML5標簽解析出錯3. 正則表達式 - nginx 正則,如何匹配不以/結尾且不以.xml .html .htm結尾4. html - css布局問題,背景用用div畫的三角形是否用absolute與z-index來定位與規定在下方是否是個好方案5. 我 想好好學精通一門技術,大家用的走過的路,幫我指點指點唄 讓我少走了彎路和坑的苦 ,自學,自己摸6. javascript - 求助Angular 跨控制器調用方法可行嗎?7. 淺談vue生命周期共有幾個階段?分別是什么?8. index.php錯誤,求指點9. Android 高德地圖如何移除添加的某個marker?10. javascript - 函數聲明和匿名函數有什么不同?(前端小白求助。。)
