package IO;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;
/**打印流:很方便的输出
*
* 字节打印流:在字节输出时候,可以增强输出功能
* 字符打印流:在字符输出时候,可以增强输出功能
*/
public class PrintStreamDemo {
public static void main(String[] args) {
bytePrint();
charPrint();
}
public static void bytePrint(){
File file = new File("c://test//3.txt");
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);
//加缓存
BufferedOutputStream buf = new BufferedOutputStream(fo);
//增强打印功能
PrintStream ps = new PrintStream(buf);
ps.println("因为喜欢,只是喜欢");
System.out.println("字节流打印成功");
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void charPrint(){
File file = new File("c://test//3.txt");
try {
Writer fi = new FileWriter(file,true);
//加缓存
BufferedWriter buf = new BufferedWriter(fi);
//增强打印功能
PrintWriter pw = new PrintWriter(buf);
pw.println("因为喜欢,只是喜欢");
System.out.println("字符流打印成功");
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java打印流的使用
最新推荐文章于 2024-07-13 08:46:01 发布