java.io.PrintWriter 中 write() 与 print() 的区别

try {  
            PrintWriter pw = response.getWriter();  
              
            int x = 98;  
              
            pw.write(x);  
              
            pw.print(x);  
              
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
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方法:

  view plaincopy to clipboardprint?
 public void write(int c) {  
try {  
    synchronized (lock) {  
 ensureOpen();  
 out.write(c);  
    }  
}  
catch (InterruptedIOException x) {  
    Thread.currentThread().interrupt();  
}  
catch (IOException x) {  
    trouble = true;  
}  
   } 
  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 plaincopy to clipboardprint?
public void print(int i) {  
rite(String.valueOf(i));  
  } 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lihan6415151528/archive/2009/02/20/3914732.aspx

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释这段代码package question7; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.Random; public class Test { public static void main(String args[]){ Test test=new Test(); Scanner input=new Scanner(System.in); String s1=new String(); s1=input.nextLine(); String[] s2 = s1.split(" "); int[] l=new int[s2.length]; for(int i=0 ; i<s2.length;i++){ l[i]=Integer.parseInt(s2[i]); } test.write("test.txt", l); int[] readlist=test.read("test.txt",l); isPrime isprime=new isPrime(readlist); for(int i=1;i<=10;i++){ new Thread(isprime).start(); } try { Thread.sleep(1000); //1000 毫秒,也就是1秒. } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } Set<Integer> set=new HashSet(); set=isprime.getSet(); System.out.println("素数set"+set); System.out.println("输入查询数字"); int num=input.nextInt(); if(set.contains(num)){ System.out.println(num+"是素数"); } else System.out.println(num+"不是是素数"); } public void write(String filename,int a[]){ File file = new File(filename); try { PrintWriter output=new PrintWriter(file); for(int i=0;i<a.length;i++){ output.print(a[i]+" "); } output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int[] read(String filename,int a[]){ File file=new File(filename); try { Scanner input=new Scanner(file); int i=0; while(input.hasNext()){ a[i]=input.nextInt(); i++; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return a; } }
最新发布
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值