Java学习笔记33(IO:打印流,IO流工具类)

打印流:

  有两个类:PrintStream     PrintWriter类,两个类的方法一样,构造方法不一样

  PrintStream构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream)

  PrintWriter构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream),接收字符输出流(Writer)

为其他流添加功能,可以方便的打印各种数据值,不同的是,他永远不会抛出IO异常

package com.zs.Demo2;

import java.io.*;

public class Demo {
    public static void main(String[] args) {
        try {
            fun1();
            fun2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void fun2() throws IOException {
        FileWriter f=new FileWriter("h:\a.txt");
        //PrintWriter接收字节输出流,也接受字符输出流
        PrintWriter p=new PrintWriter(f);
        p.write("hello java");
        p.close();
    }

    private static void fun1() throws IOException {
        FileOutputStream f=new FileOutputStream("h:\a.txt");
        //PrintStream只接收字节输出流,接收字符输出流时会报错
        PrintStream p=new PrintStream(f);
        p.write(101);//输出编码后的字符e
        p.print(101);//输入什么就是输出什么
        p.write("hello world
".getBytes());
        p.print("hello java");
        p.close();
    }
}

打印流复制文件,自动刷新

package com.zs.Demo2;

import java.io.*;

public class Demo2 {
    public static void main(String[] args) {
        try {
            fun();
            fun2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //打印流自动刷新
    private static void fun2() throws FileNotFoundException {
        //从下面可以看出,printWriter可以接收字节输出流
        PrintWriter pw=new PrintWriter(new FileOutputStream("h:\a.txt"),true);
        //构造方法中第一个参数是文件类型,第二个参数true是是否自动刷新,不需要写flush方法
        pw.write("hello world");
        pw.close();
    }

    //打印流复制文件
    private static void fun() throws IOException {
       BufferedReader fr=new BufferedReader(new FileReader("h:\a.txt"));
//     PrintWriter pw=new PrintWriter("h:\b.txt",true);报错,开启自动刷新必须参数为File类型
       PrintWriter pw=new PrintWriter(new FileWriter("h:\b.txt"),true);
       String s=null;
        while ((s=fr.readLine())!=null) {
            pw.write(s);
        }
        pw.close();
        fr.close();
    }
}

IO流操作工具类:commons工具类(在网上下载)

  它是一个操作IO流的类,可以简化代码,就像数组的操作类Arrays,集合操作类Collections类一样,这是IO流操作类,使用前要构建路径,在当前项目下新建lib文件夹,放入commons包,然后eclipse右击该包点击构架路径(buildpath),idea导包:项目下建lib文件夹,放入要导入的包,右击包,Add as library..

package com.zs;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;

public class Demo3 {
    public static void main(String[] args) {
        try {
            fun();//文件操作
            fun2();//文件名操作
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void fun2() {
        //获得文件名
        String name = FilenameUtils.getName("g:\111\a.txt");
        System.out.println(name);//a.txt
    //获得后缀,可以输入文件地址,
        String name2 = FilenameUtils.getExtension(name);
        System.out.println(name2);//txt
        //判断文件后缀
        boolean b = FilenameUtils.isExtension("c.java", "java");
        System.out.println(b);//true

    }

    private static void fun() throws IOException {
        //写字符串到文件
        FileUtils.writeStringToFile(new File("h:\a.txt"),"java");
        //读文件内容
        String s = FileUtils.readFileToString(new File("h:\a.txt"));
        System.out.println(s);
        //复制文件夹
        FileUtils.copyDirectory(new File("e:\java.jar"),new File("h:\赵帅\java.jar"));
        //复制一个文件夹到另一个文件夹,要复制到的文件夹不存在时,会新建文件夹
        FileUtils.copyDirectoryToDirectory(new File("h:\赵帅\linux"),new File("e:\cz"));
        //复制文件
        FileUtils.copyFile(new File("h:\a.txt"),new File("g:\a.txt"));

    }
}
原文地址:https://www.cnblogs.com/Zs-book1/p/10608361.html