Java I/O框架(流(下))

字符流

  • 字符流的父类(抽象类):
    • Reader:字符输入流
    • Writer:字符输出流
  • 文件字符流
    • FileReader
    • FileWriter
  • 只能复制文本文件,不能复制图片等二进制文件
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("d:\\hello.txt");
//        int data = 0;
//        //一个字符3个字节
//        while ((data=fr.read())!=-1){
//            System.out.print((char) data);
//        }
        char[] buf = new char[1024];
        int count = 0;
        while ((count=fr.read(buf))!=-1){
            System.out.println(new String(buf,0,count));
        }
        fr.close();
    }
    public static void main(String[] args) throws Exception{
        FileWriter fw = new FileWriter("d:\\writer.txt");
        for (int i = 0; i < 10; i++) {
            fw.write("java best\r\n");
            fw.flush();
        }
        fw.close();
    }

字符缓冲流

  • BufferedReader/BufferedWriter
    • 高效读写
    • 支持输入换行符
    • 可一次写一行,读一行
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("d:\\hello.txt");
        BufferedReader br = new BufferedReader(fr);
        //一行一行读
        String s = null;
        while ((s=br.readLine())!=null){
            System.out.println(s);
        }
        br.close();
    public static void main(String[] args) throws Exception{
        FileWriter fw = new FileWriter("d:\\aaa.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        for (int i = 0; i < 10; i++) {
            bw.write("好好学习,天天享受");
            //换行
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

打印流

  • PrintWriter:
    • 封装了print()/println()方法,支持写入后换行
    • 支持数据原样打印
    public static void main(String[] args) throws Exception{
        PrintWriter pw = new PrintWriter("d:\\print.txt");
        pw.println(97);
        pw.println(true);
        pw.print(3.14);
        pw.print('a');
        pw.close();
    }

转换流

  • 桥转换流:InputStreamReader/OutputStreamReader
    • 可将字节流转换为字符流
    • 可设置字符的编码方式
  • 保证文件编码和指定编码一致
public class Demo14 {
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("d:\\wr.txt");
        InputStreamReader isr = new InputStreamReader(fis, "gbk");
        char[] buf = new char[1024];
        int count = 0;
        while ((count=isr.read(buf))!=-1){
            System.out.println(new String(buf,0,count));
        }
        isr.close();
    }
    public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("d:\\out.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
        for (int i = 0; i < 10; i++) {
            osw.write("最好的语言\r\n");
        }
        osw.close();
    }

File类

  • 概念:代表物理盘符中的一个文件或者文件夹
public class Demo1 {
    public static void main(String[] args) throws Exception{
        separator();
        fileOpe();
        directoryOpe();
    }
    //分隔符
    public static void separator(){
        System.out.println("路径分隔符"+ File.pathSeparator);
        System.out.println("名称分隔符"+File.separator);
    }
    //文件操作
    public static void fileOpe() throws Exception{
        //创建文件
        // 创建文件对象(在硬盘上不存在)
        File file = new File("d:\\szx.txt");
        //System.out.println(file);
        if (!file.exists()) {
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
        }

        //删除文件
        //直接删除
        //boolean delete = file.delete();
        //System.out.println(delete);
        //使用jvm退出时删除
        //file.deleteOnExit();
        //Thread.sleep(5000);

        //获取文件信息
        System.out.println("绝对路径:"+file.getAbsolutePath());
        System.out.println("获取路径:"+file.getPath());
        System.out.println("获取文件名称:"+file.getName());
        System.out.println("获取父目录:"+file.getParent());
        System.out.println("获取文件长度:"+file.length());
        System.out.println("获取文件创建时间:"+new Date(file.lastModified()).toLocaleString());

        //判断
        System.out.println("是否可写:"+file.canWrite());
        System.out.println("是否是文件:"+file.isFile());
        System.out.println("是否隐藏:"+file.isHidden());
    }
    //文件夹操作
    public static void directoryOpe() throws Exception{
        File dir = new File("d:\\aaa\\bbb\\ccc");
        if (!dir.exists()){
            //mkdirs可以创建多级目录
            boolean newFile = dir.mkdirs();
            System.out.println(newFile);
        }
        //只删除最底层,且最底层必须是空文件夹
        //boolean delete = dir.delete();
        //System.out.println("删除结果:"+delete);
        dir.deleteOnExit();
        Thread.sleep(5000);

        System.out.println("绝对路径:"+dir.getAbsolutePath());
        System.out.println("获取路径:"+dir.getPath());
        System.out.println("获取文件名称:"+dir.getName());
        System.out.println("获取父目录:"+dir.getParent());
        System.out.println("获取文件创建时间:"+new Date(dir.lastModified()).toLocaleString());

        System.out.println("是否是文件夹"+dir.isDirectory());
        System.out.println("是否隐藏"+dir.isHidden());

        //遍历文件夹 list
        File f = new File("c:\\Users\\13672\\Pictures\\哔哩哔哩动画");
        String[] list = f.list();
        System.out.println("------------------------");
        for (String s : list) {
            System.out.println(s);
        }
    }
}

FileFilter接口

  • public interfaceFileFilter
    • boolean accept(File pathname)
  • 当调用File类中的listFiles()方法时,支持传入FileFilter接口实现类,对获取文件进行过滤,只有满足条件的文件才可出现在listFiles()的返回值中
        File[] file2 = f.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.getName().endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        });
        for (File file : file2) {
            System.out.println(file.getName());

递归遍历文件夹,递归删除文件夹

public class ListDemo {
    public static void main(String[] args) {
        listDir(new File("d:\\aaa"));
        deleteDir(new File("d:\\aaa"));
    }
    public static void listDir(File dir){
        File[] files = dir.listFiles();
        //System.out.println(dir.getAbsolutePath());
        //System.out.println(files.length);
        if (files!=null && files.length>0){
            for (File file : files) {
                if (file.isDirectory()){
                    listDir(file);
                }else{
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
    //递归删除文件夹
    public static void deleteDir(File dir){
        File[] files = dir.listFiles();
        if (files!=null && files.length>0){
            for (File file : files) {
                if(file.isDirectory()){
                    deleteDir(file);
                }else{
                    //删除文件
                    System.out.println(file.getAbsolutePath()+"删除成功"+file.delete());
                }
            }
        }
        System.out.println(dir.delete());
    }
}

Properties集合

  • 特点:
    • 存储属性名和属性值
    • 属性名和属性值都是字符串类型
    • 没有泛型
    • 和流有关
public class Demo4 {
    public static void main(String[] args) throws Exception{
        Properties properties = new Properties();
        properties.setProperty("username","zhangsan");
        properties.setProperty("age","20");
        System.out.println(properties.toString());
        //遍历
//        Set<String> strings = properties.stringPropertyNames();
//        for (String string : strings) {
//            System.out.println(string+"----"+properties.getProperty(string));
//        }

        //和流有关的方法
        //----list----------
//        PrintWriter printWriter = new PrintWriter("d:\\print.txt");
//        properties.list(printWriter);
//        printWriter.close();
//        //--------store--------
//        FileOutputStream fos = new FileOutputStream("d:\\print.properties");
//        properties.store(fos,"注释");
//        fos.close();
        //--------load-----------
        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("d:\\print.properties");
        properties1.load(fis);
        fis.close();
        System.out.println(properties1.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值