zoukankan      html  css  js  c++  java
  • java 打印流与commons-IO

    一 打印流

    1.打印流的概述

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

    打印流根据流的分类:

      字节打印流 PrintStream

      字符打印流 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();
        }
    }

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

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

    构造方法:

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

      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();
        }
    }

    二 commons-IO

    1.FileUtils

    提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。

       常用方法:

      readFileToString(File file):读取文件内容,并返回一个String;

      writeStringToFile(File file,String content):将内容content写入到file中;

      copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制

      copyFile(File srcFile,File destFile);文件复制

    代码演示:

    /*
     * 完成文件的复制
     */
    public class CommonsIODemo01 {
        public static void main(String[] args) throws IOException {
            //method1("D:\test.avi", "D:\copy.avi");
            
            //通过Commons-IO完成了文件复制的功能
            FileUtils.copyFile(new File("D:\test.avi"), new File("D:\copy.avi"));
        }
    
        //文件的复制
        private static void method1(String src, String dest) throws IOException {
            //1,指定数据源 
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
            //2,指定目的地
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
            //3,读
            byte[] buffer = new byte[1024];
            int len = -1;
            while ( (len = in.read(buffer)) != -1) {
                //4,写
                out.write(buffer, 0, len);
            }
            //5,关闭流
            in.close();
            out.close();
        }
    }
    
    /*
     * 完成文件、文件夹的复制
     */
    public class CommonsIODemo02 {
        public static void main(String[] args) throws IOException {
            //通过Commons-IO完成了文件复制的功能
            FileUtils.copyFile(new File("D:\test.avi"), new File("D:\copy.avi"));
            
            //通过Commons-IO完成了文件夹复制的功能
            //D:基础班 复制到 C:\abc文件夹下
            FileUtils.copyDirectoryToDirectory(new File("D:\基础班"), new File("C:\abc"));
        }
    }
  • 相关阅读:
    用ssh整合时,用sessionfactory的getCurrentSession()获取不到session
    layim+signalr2.0+mongodb在线轻聊版解决方案(可提供演示)
    (绿色)修正版gooflow流程解决方案(源码分享+在线演示+UI地址下载)
    一个开源的可视化的jQuery工作流插件
    自定义流程gooflow.08 demo在线演示
    asp.net mvc 系统操作日志设计
    第三方系统平台如何对接gooflow2.0
    一个供新手把玩的jQueryUI在线文档
    Ninject 自动注册
    JQuery.imgAreaSelect 参数说明
  • 原文地址:https://www.cnblogs.com/jiejava/p/13450252.html
Copyright © 2011-2022 走看看