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

本文详细解析了Java中PrintWriter类的write()与print()方法的区别。write()方法主要用于处理字符及字符数组,而print()方法能将多种数据类型转换为字符串形式输出。
 

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

标签: c
  5534人阅读  评论(0)  收藏  举报
  分类:
 

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

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/lihan6415151528/archive/2009/02/20/3914732.aspx

### ### `PrintWriter.print()` `PrintWriter.println()` 方法的区别 `PrintWriter.print()` 和 `PrintWriter.println()` 是 Java 中用于向输出流写入数据的两个常用方法,二者在功能和使用场景上存在显著差异。 #### 输出行为 `PrintWriter.print()` 方法用于将指定的数据写入到输出流中,但不会在写入内容的末尾自动添加换行符。该方法支持多种数据类型,包括字符串、字符、整数、浮点数等,适用于需要连续输出多个数据而不换行的场景。例如: ```java PrintWriter writer = new PrintWriter("output.txt"); writer.print("Hello"); writer.print("World"); writer.close(); ``` 上述代码执行后,文件 `output.txt` 中的内容为 `HelloWorld`,两个字符串被连续写入,中间没有换行。 之不同,`PrintWriter.println()` 方法除了将数据写入输出流外,还会在写入内容的末尾添加一个换行符。这使得每次调用 `println()` 后,后续写入的内容会出现在新的一行。例如: ```java PrintWriter writer = new PrintWriter("output.txt"); writer.println("Hello"); writer.println("World"); writer.close(); ``` 执行后,文件 `output.txt` 中的内容为: ``` Hello World ``` 每条输出内容独占一行,便于阅读和后续处理。 #### 内部实现机制 `PrintWriter.println()` 的实现本质上是在调用 `print()` 方法后,再调用 `write("\n")` 添加换行符。这种方式确保了输出内容的格式化,尤其适用于日志记录、文本文件生成等需要结构化输出的场景[^2]。 #### 缓冲刷新 默认情况下,`PrintWriter` 是不自动刷新的,即写入的数据会暂存于缓冲区中,直到缓冲区满或手动调用 `flush()` 方法才会真正写入目标流。然而,当使用 `PrintWriter` 构造函数并传入 `true` 作为自动刷新参数时,`println()` 方法会在每次调用后自动刷新缓冲区,而 `print()` 则不会触发自动刷新。因此,在网络通信或实时日志输出等场景中,使用 `println()` 更加可靠,可以确保数据及时发送或写入[^5]。 --- ### ### 示例代码 以下是一个简单的 `PrintWriter` 示例,演示 `print()` `println()` 的区别: ```java import java.io.PrintWriter; public class PrintWriterExample { public static void main(String[] args) { PrintWriter writer = new PrintWriter(System.out); writer.print("Print without newline"); writer.print("Continued on same line"); writer.println(); writer.println("Print with newline"); writer.println("Another line"); writer.flush(); writer.close(); } } ``` 输出结果为: ``` Print without newlineContinued on same line Print with newline Another line ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值