打印流可以实现基本数据类型的数据格式转换为字符串输出
目录
打印流的使用
提供了一系列重载的 print()和 println()方法,用于多种数据类型的输出
PrintStream 和 PrintWriter 有自动 flush 功能,其中 PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节,在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类
代码举例
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintTest {
@Test
public void PrintTest(){
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("hello.txt"));
ps = new PrintStream(fos,true);
if (ps != null){// 把标准输出流改成文件
System.out.println(ps);
}
for (int i = 0; i < 255; i++) {//输出 ASCII 字符
System.out.println((char)i);
if (i % 50 == 0){
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null)
ps.close();
}
}
}