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. javascript - 關于apply()與call()的問題2. java - 在用戶不登錄的情況下,用戶如何添加保存到購物車?3. javascript - webpack 分割加載代碼后,react 界面不更新4. python 利用subprocess庫調用mplayer時發生錯誤5. datetime - Python如何獲取當前時間6. python文檔怎么查看?7. javascript - nginx反向代理靜態資源403錯誤?8. html - eclipse 標簽錯誤9. 安全性測試 - nodejs中如何防mySQL注入10. java - spring boot 如何打包成asp.net core 那種獨立應用?
