Socket PrintWriterでのwrite()とprint()の違い

1011 ワード

try {
			PrintWriter pw = response.getWriter();
			
			int x = 98;
			
			pw.write(x);
			
			pw.print(x);
			
		} catch (IOException e) {
			e.printStackTrace();
		}

 
出力:b 98
最終的には抽象クラスWriterのwriteメソッドprintメソッドを書き換えて様々なタイプのデータを文字列に変換して出力することができます.リロードされたwriteメソッドは、文字、文字配列、文字列などの文字に関連するデータしか出力できません.ソースコード(java.io.PrintWriter)を確認します.
1:writeメソッド:
 public void write(int c) {  
  • try {  

  •     synchronized (lock) {  
  •  ensureOpen();  

  •  out.write(c);  
  •     }  

  • }  
  • catch (InterruptedIOException x) {  

  •     Thread.currentThread().interrupt();  
  • }  

  • catch (IOException x) {  
  •     trouble = true;  

  • }  
  •    }  

  •  
    2:printメソッド:
      
    view plain copy to clipboard print ?
    public void print(int i) {  
  • rite(String.valueOf(i));  

  •   }