加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

打印流相关

发布时间:2021-05-20 09:44:37 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; /* ?* 打印流 ?* PrintStream ?* PrintWriter ?* 打印流的特点:①此
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/*
?* 打印流
?* PrintStream
?* PrintWriter
?* 打印流的特点:①此流不负责数据源,只负责数据目的端;②为其他输出流,添加功能;
?* ?? ??? ??? ?③永远不会抛出IOException,但是可能抛出别的异常
?* 两个打印流的方法完全一致,区别在于构造方法上
?* 构造方法就是打印流的输出目的端
?* PrintStream
?*?? 构造方法,接收File类型,接收字符串文件名,接收字节输出流OutputStream
?* PrintWriter
?* ?? ??? 构造方法,接收File类型,接收字符串文件名,接受字节输出流OutputStream,接收字符输出流Writer
?*/
public class PrintWriterDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?function_3();
?? ?}
?? ?
?? ?/*
?? ? * 打印流,向File对象的数据目的写入数据
?? ? * 方法:print/println? 原样输出?????? ?
?? ? *???? write方法走adcii码表
?? ? */
?? ?public static void function() throws FileNotFoundException{
?? ??? ?File file=new File("d:demo1.txt");
?? ??? ?PrintWriter pw=new PrintWriter(file);
?? ??? ?pw.println(100);
?? ??? ?pw.write(100);
?? ??? ?//pw.flush();
?? ??? ?pw.close();
?? ?}
?? ?
?? ?/*
?? ? * 打印流,输出目的,String文件名
?? ? */
?? ?public static void function_1() throws FileNotFoundException{
?? ??? ?PrintWriter pw=new PrintWriter("d:demo2.txt");
?? ??? ?pw.println(3.5);
?? ??? ?pw.close();
?? ?}
?? ?
?? ?/*
?? ? * 打印流,输出目的,是流对象
?? ? * 可以是字节输出流,可以是字符的输出流
?? ? * OutputStream Writer
?? ? */
?? ?public static void function_2() throws IOException{
?? ??? ?//FileOutputStream fos=new FileOutputStream("d:demo3.txt");
?? ??? ?FileWriter fw=new FileWriter("d:demo4.txt");
?? ??? ?PrintWriter pw=new PrintWriter(fw);
?? ??? ?pw.println("打印流");
?? ??? ?pw.close();
?? ?}
?? ?
?? ?/*
?? ? * 打印流,可以开启自动刷新功能
?? ? * 满足2个条件:①输出的数据目的必须是流对象?? OutputStream Writer;
?? ? * ?? ??? ??? ?②必须调用println,printf,format三个方法中的一个,才能启用自动刷新
?? ? */
?? ?public static void function_3() throws IOException{
?? ??? ?FileOutputStream fos=new FileOutputStream("d:demo5.txt");
?? ??? ?PrintWriter pw=new PrintWriter(fos,true); //true开启自动刷新功能
?? ??? ?pw.println("a");
?? ??? ?pw.println("b");
?? ??? ?pw.println("c");
?? ??? ?pw.close();
?? ?}
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读