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

    一.打印流

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

    打印流根据流的分类:

    1.字节打印流 PrintStream

    2.字符打印流 PrintWriter

    方法:

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

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

     /* 
     * 需求:把指定的数据,写入到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();
        }
    }

    二.打印流完成数据自动刷新

    构造方法:

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

    public PrintWriter(OutputStream out, boolean autoFlush)

    public PrintWriter(Writer out, boolean autoFlush)

     /* 
     * 分析:
     *     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();
        }
    }
  • 相关阅读:
    git安装
    git
    运维项目维护个人总结经验
    redis基本命令
    mysql基础常用命令
    进入Linux单用户模式
    Nginx查看并发链接数
    linux编写脚本检测本机链接指定IP段是否畅通
    集体干死java 在启动.sh
    系统优化小脚本
  • 原文地址:https://www.cnblogs.com/akiyama/p/10250613.html
Copyright © 2011-2022 走看看