zoukankan      html  css  js  c++  java
  • IO流之打印流

    打印流的概述(只有输出就是只与数据目的有关,不会抛出IO异常)

    打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.

    打印流根据流的分类:

    l  字节打印流  PrintStream

    l  字符打印流  PrintWriter

    l  方法:

    void print(String str): 输出任意类型的数据,

    void println(String str): 输出任意类型的数据,自动写入换行操作

    l  代码演示:

     /* 
     * 需求:把指定的数据,写入到printFile.txt文件中
     * 
     * 分析:
     * 	1,创建流
     * 	2,写数据
     * 	3,关闭流
     */
    public class PrintWriterDemo {
    	public static void main(String[] args) throws IOException {
    		//创建流
    		//PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));
    		PrintWriter out = new PrintWriter("printFile.txt");
    		//2,写数据
    		for (int i=0; i<5; i++) {
    			out.println("helloWorld");
    		}
    		//3,关闭流
    		out.close();
    	}
    }
    

    打印流完成数据自动刷新

    可以通过构造方法,完成文件数据的自动刷新功能

    l  构造方法:

    l  开启文件自动刷新写入功能

    public PrintWriter(OutputStream out, boolean autoFlush)

    public PrintWriter(Writer out, boolean autoFlush)

    l  代码演示:

     /* 
     * 分析:
     * 	1,创建流
     * 	2,写数据
     */
    public class PrintWriterDemo2 {
    	public static void main(String[] args) throws IOException {
    		//创建流
    		PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true);
    		//2,写数据
    		for (int i=0; i<5; i++) {
    			out.println("helloWorld");
    		}
    		//3,关闭流
    		out.close();
    	}
    }
    

      

      

  • 相关阅读:
    DIV指令一般用法
    "Programming"和"Programming"是同一个"Programming"吗?
    对5个国家的名称进行排序详细解析
    Hello World程序
    用 select 实现多选
    浅谈HTTP中Get与Post的区别
    ligerUI 下拉框表格(多选)
    LigerUI自带弹窗返回值例子
    LigerUI Grid服务端分页技术
    jQuery LigerUI 插件介绍及使用之ligerGrid
  • 原文地址:https://www.cnblogs.com/lxx2014/p/9543130.html
Copyright © 2011-2022 走看看