最全JAVA-IO知识点解读

1.文件遍历

public class Demo4 {
    public static void main(String[] args) {
        File e = new File("e://");
        File[] files = e.listFiles();
        listFile(files);

    }

    public static void listFile(File[] files){
        if(files!=null&&files.length>0){
            for (File file:files) {
                if(file.isFile()){
                    if(file.getName().endsWith(".avi")){
                        if(file.length()>100*1024*1024)
                        System.out.println("找到了一个avi文件"+file.getAbsolutePath());
                    }
                }else {
                    File[] files2 = file.listFiles();
                    listFile(files2);
                }
            }
        }

    }
}

2.相对路径和绝对路径

绝对路径:从盘符开始。是一个完整的路径

相对路径:在java代码中是相对于项目目录路径,这是一个不完整的便捷路径

3.流概述

IO流概述
*  可以将这种数据传输操作,看做一种数据的流动 , 按照流动的方向分为输入Input和输出Output
*  Java中的IO操作主要指的是 java.io包下的一些常用类的使用. 通过这些常用类对数据进行读取(输入Input) 和 写出(输出Output)
*
* IO流的分类:
*  按照流的方向来分,可以分为:输入流和输出流.
*  按照流动的数据类型来分,可以分为:字节流和字符流
*
*     字节流:
*          -   输入流 :   InputStream
*          -   输出流 :   OutputStream
*     字符流:
*          -   输入流 :   Reader
*          -   输出流 :   Writer

4.字节流-OutputStream

public static void main(String[] args) throws IOException {
        //OutputStream
        FileOutputStream fos = new FileOutputStream("c://a.txt");
        /*byte[] bytes = {65,66,67,68,69};
        fos.write(bytes);*/
        //byte[] bytes2 = {65,66,67,68,69};
        byte[] bytes2 = "ABCDEF".getBytes();
        fos.write(bytes2,2,2);
        //fos.write(bytes2);
        fos.close();                //写在哪在哪关闭
        System.out.println("已经写出");

    }

[一个int=4个字节  一个字节=8bit]

5.FileOutputStream

1.添加单个字节

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("d://a.txt");
        fos.write(65);
        fos.close();
        System.out.println("已经写出");
    }
}
/*
添加数组
*/
public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("d://a.txt");
        //一次添加一个数组
        byte[] bytes = {65,66,67};
        fos.write(bytes);
        fos.close();
        System.out.println("已经写出");
        //输出ABC
    }
}

3.添加局部字节

public class Demo2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("d://a.txt");
        //添加局部字节
        byte[] bytes = "ABCDE".getBytes(StandardCharsets.UTF_8);
        fos.write(bytes,1,2);
        fos.close();
        System.out.println("已经写出");
        //输出BC
    }
}

6.FileinputStream

##(快捷注释:ctrl+shift+/)

public class Demo7 {
    //InputStream
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("d://a.txt");//传对象或者传路径;
        /*while (true){
            byte b = (byte) fis.read();
            if(b==-1){
                break;
            }
            System.out.println((char) b);
        }*/
        byte[] bytes = new byte[10];
        int len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        len = fis.read(bytes);
        System.out.println(new String(bytes,0,len));
        fis.close();
    }
}

7.字符输出

import java.io.FileWriter;
import java.io.IOException;


public class Demo9 {
    public static void main(String[] args) throws IOException {
        //writer
        FileWriter fw = new FileWriter("e://b.txt",true);
        //fw.write('a');
        fw.append("锄禾日当午").append(",").append("汗滴禾下土");   //可以一致追加
        fw.write("锄禾日当午");
        fw.flush();        //刷新缓存空间,写入文件
        fw.close();

    }
}

8.字符读取

public class Demo10 {
    public static void main(String[] args) throws IOException {
        //reader
        FileReader fr = new FileReader("e://b.txt");
        while (true){
            int c = fr.read();
            if(c==-1){
                break;
            }
            System.out.println((char)c);
        }
        char[] chars = new char[100];
        //fr.read(chars);
        System.out.println(chars[0]);

        fr.close();
    }
}

9.字节流装饰为字符流(字符流要刷新(flash)管道)

public class Demo11 {
    public static void main(String[] args) throws IOException {
        //转换流  :将字节流转换成字符流     使用了装饰者模式
        FileInputStream fis = new FileInputStream("c://a.txt");
        //将字节输入流转换为字符输入流  参数为要转换的字节流
        InputStreamReader isr = new InputStreamReader(fis,"gbk");
        while (true){
            int c = isr.read();
            if(c==-1){
                break;
            }
            System.out.println((char) c);
        }

    }
}

10.收集异常日志

public class Demo12 {
    public static void main(String[] args) throws FileNotFoundException {
        //收集异常信息
        try {
            String s = null;
            s.toString();
        }catch (Exception e){
            PrintWriter pw = new PrintWriter("e://bug.txt");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");//创建日期
            pw.print(format.format(new Date()));
            e.printStackTrace(pw);
            pw.close();
        }


    }
}

11.properties

public class Demo13 {
    public static void main(String[] args) throws IOException {
        //properties文件与properties类
        
        /*
        在e盘创建
         */
        Properties pt = new Properties();
        //键=值
        pt.put("name","金苹果");
        pt.put("info","讲述历了金苹果种植的过程");
        FileWriter fw = new FileWriter("e://book.properties");
        pt.store(fw,"存储的图书");
        fw.close();
        
        /*
        从e盘读取输出
         */
        Properties pt = new Properties();
        Reader fw = new FileReader("e://book.properties");
        pt.load(fw);
        System.out.println (pt.getProperty("name"));
        System.out.println("info");
    }
}

12.

try-with-resources

FileReader fr = new FileReader("c://book.txt");
        PrintWriter pw = new PrintWriter("c://book.txt");
        try(fr;pw){
            int c = fr.read();
            System.out.println((char)c);
        }catch (IOException e) {
            e.printStackTrace();
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值