zoukankan      html  css  js  c++  java
  • Java—io流之打印流、 commons-IO

    打印流

      打印流根据流的分类:

      字节打印流  PrintStream

      字符打印流  PrintWriter

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

     

    复制文件

    复制代码
    public class Demo02 {
    public static void main(String[] args) throws IOException {
        //文件复制
        FileReader fr=new FileReader("D:\io0512\print.txt");
        //添加缓冲流
        BufferedReader br=new BufferedReader(fr);
        //明确目的地
        FileWriter fw=new FileWriter("D:\io0512\a\print.txt");//续写加true
        //添加打印流
        PrintWriter pw=new PrintWriter(fw,true);//开启文件自动刷新写入功能
        //开始复制
        String len=null;
        while((len=br.readLine())!=null){
            pw.println(len);
        }
        br.close();
        pw.close();
        
    }
    }
    复制代码

    commons-IO

      导入classpath

        加入classpath的第三方jar包内的class文件才能在项目中使用

        创建lib文件夹

        将commons-io.jar拷贝到lib文件夹

        右键点击commons-io.jar,Build Path→Add to Build Path

      FilenameUtils

        这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题

        常用方法:

          getExtension(String path):获取文件的扩展名;

          getName(String filename):获取文件名;

          isExtension(String fileName,String ext):判断fileName是否是ext后缀名;

    复制代码
    public class Demo03 {
    public static void main(String[] args) {
        //获取文件拓展名,打印txt
        String ext=FilenameUtils.getExtension("D:\io0512\print.txt");
        System.out.println(ext);
        //获取文件名,打印print.txt
        String filename=FilenameUtils.getName("D:\io0512\print.txt");
        System.out.println(filename);
        //判断filename是否是ext后缀名
        boolean flsg=FilenameUtils.isExtension(filename, ext);
        System.out.println(flsg);
    }
    }
    复制代码

    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"));
        }
    }
    复制代码
  • 相关阅读:
    C# Debugger.IsAttached 调试启动浏览器 VS if DEBUG 启动调试内容
    【转载】如何三个月从零基础到C#中级程序员
    【转载】如何成为一个高级程序员
    如何找回QQ聊天记录、语音、图片?
    hexo博客yili主题个性化自定义教程(1) ——借鉴中学习,初认yili主题
    2019hexo博客部署到coding该绕的坑-奥怪的小栈
    2019Hexo博客Next主题深度美化 打造一个炫酷博客(2)-奥怪的小栈
    2019软件工程专业大学排名附官网-奥怪的小栈
    浅谈互联网+足球
    2019基于Hexo快速搭建个人博客,打造一个炫酷博客(1)-奥怪的小栈
  • 原文地址:https://www.cnblogs.com/wode007/p/13434696.html
Copyright © 2011-2022 走看看