PrintStream
- 字节打印流PrintStream
- 运用实列
public static void main(String[] args) throws IOException {
//out是一个PrintStream类
PrintStream out = System.out;
//在默认情况下PrintStream输出数据的位置是标准输出:显示器
out.println("hello,world1");
//print底层调用的是write方法,所以这里输出也可以直接用write方法
out.write("hello,world2".getBytes());//getBytes将字符串转换为字节数组
//注意这里的close方法不写,输入的内容是不会刷新的
//flush()+关闭流,才会把数据写入文件中
out.close();
//打印流,我们可以更改输出的位置或者设备
System.setOut(new PrintStream("E:/IOFile/news5.txt"));
//setOut地产调用native方法setOut(),修改了out
System.out.println("hello,world3");
}
PrintWriter
- 字符打印流PrintWriter
2. 运用实列
public static void main(String[] args) throws IOException {
// PrintWriter pipedWriter = new PrintWriter(System.out);
PrintWriter p = new PrintWriter(new FileWriter("E:/IOFile/news6.txt"));
p.write("hello");
p.close();
}