shuijibaobao

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

IO流

存储和读取数据的解决方案
用于读写文件中的数据(本地文件,网络中的数据...)
分类:

  1. 按流向:
    1. 输入流
    2. 输出流
  2. 按操作文件的类型
    1. 字节流
    2. 字符流

FileOutputStream

    public static void main(String[] args) throws IOException{
        FileOutputStream fos = new FileOutputStream("myio\\a.txt");
        fos.write(97);
        fos.close();
    }

//创建对象的细节:
//1: 参数是字符中表示的路径或者是File对象都是可以的
//2: 如果文件不存在会创建一个新的文件,但是要保证父级路径是存在的。
//3: 如果文件存在,会清空文件

//写出数据:
//write方法的参数是整数,但是实际上写到本地文件中的是整数在ASCII上对应的字符
//97-->a   57-->9  55-->7(懂我意思不)

FileOutputStream写数据的3种方式

/*
    void write(int b)  一次写一个字节数据
    void write(byte[] b)  一次写一个字节数组数据
    void write(byte[] b, int off, int len)  一次写一个字节数组的部分数据
    参数一:数组 
    参数二:起始索引 
    参数三:个数
*/
    byte[] bytes = {97, 98, 99, 100, 101};

    fos.write(97);
    fos.write(97, 98, 99, 100);//a b c d 
    fos.write(bytes, 1 ,2);//b c

FileOutputStream写数据的两个小问题捏

换行写:

//换行写:
//写一个换行符
//windows:  \r\n
//linux:  \n
//mac:  \r
//细节:\r \n 写一个也行,但是尽量写全
    String str = "kankelaoyezuishuai";
    byte[] bytes = str.getBytes();
    fos.write(bytes);
    fos.close();

    String wrap = "\r\n";
    byte[] bytes2 = wrap.getBytes();
    fos.write(bytes2);

    String str2 ="666";
    byte[] bytes3 = str2.getBytes();
    fos.write(bytes3);

续写:

/*如果想要续写。打开续写开关即可开关位置;
创建对象的第二个参数默认false:  表示关闭续写,此时创建对象会清空文件手动传递
true:表示打开续写,此时创建对象不会清空文件
*/    
    FileOutputStream fos = new FileOutputStream("myio\\a.txt", true);

FileInputStream

    FileInputStream fis = new FileInputStream("myio\\a.txt");

    int b1 = fis.read();

    //几个数据读几次

    //读不到返回-1
    System.out.println(b1);
    
    System.out.println(char(b1));
    //转换成char类型

    fis.close();

/*细节:
    1: 创建对象
        如果文件不存在,就直接报错,即使创建出来,也是没有数据的,没有任何意义
    2:写数据
        细节1:一次读一个字节,读出来的是数据在ASCII上对应的数字
        细节2:读到文件末尾了,read方法返回-1。
        空格是32, -1分开读

FileInputStream循环读取

    FileInputStream fis = new FileInputStream( name: "myio\\a.txt");

    int b;
//直接写read(),一次循环指针动两次
//abcde  输出98 100 -1
    while((b = fis.read()) != -1){
        System.out.println(char(b));
    }
    fis.close();

文件拷贝

//  把D:\itheima\movie.mp4拷贝到当前模块下

    FileInputStream fis = new FileInputStream("D:\\itheima\\movie.mp4");
    FileOutputStream fos = new FileOutputStream("myio\\copy.mp4");
//2.拷贝
//核心思想: 边读边写
    int b;
    while((b = fis.read()) != -1){
        fos.write(b);
    }
//释放资源
//规则:新开的最后关闭
    fos.close();
    fis.close();

FileInputStream读取的问题

//FileInputStream一次读写一个字节
    byte[] bytes = new byte[2];//一次读取多个字节数据,具体读多少,跟数组的长度有关//返回值:本次读取到了多少个字节数据
    int len1 = fis.read(bytes);
    System.out.println(len1);//2
    String str1 = new String(bytes, 0, len1);
    System.out.println(str1);

    int len2 = fis.read(bytes);
    System.out.println(len2);//2
    String str2 = new String(bytes, 0, len2);
    System.out.println(str2);

    int len3 = fis.read(bytes);
    System.out.println(len3);//1
    String str3 = new String(bytes, 0, len3);
    System.out.println(str3);//d


//读abcde 多次读取 输出是:ab cd ed 
//d没被覆盖
//改一下String里的参数
    fis.close();

文件拷贝改写

//1.创建对象
    FileInputStream fis = new FileInputStream("D:\\itheima\\movie.mp4");
    FileOutputStream fos = mew FileOutputStream( name: "myio\\copy.mp4");//2.拷贝
    int len;
    byte[] bytes = new byte[1024 * 1024 * 5]; //5mb
    while((len = fis.read(bytes)) != -1){
        fos.write(bytes, 0, len);
    }
//3.释放资源
    fos.close();
    fis.close();

//会快超多

try...catch异常处理

//被finally控制的语句一定会执行,除非JVM退出

    try(){

    }catch(){

    }finally{

    }

img
简化方案:(了解)

//AutoCloseable接口,特定的情况下,自动释放资源

    public static void main(String[] args) throws FileNotFoundException{
//JDK9:IO流中捕获异常的写法
        FileInputStream fis = new FileInputStream("D: litheima\\movie.mp4");
        FileOutputStream fos = new FileOutputstream("myio\\copy.mp4");
        try(fis;fos) {
            //2.拷贝
            int len;
            byte[] bytes = new byte[124 * 124 * 5];
            while ((len = fis.read(bytes)) != -1){
                fos.write(bytes, 0, len);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

字符流

GB2312字符集
GBK字符集(windows默认使用)

  • 规则1:汉字两个字节存储
  • 规则2:高位字节二进制一定以1开头,转成十进制之后是一个负数
  • 规则3:英文一个字节存储,兼容ASCII,二进制前面补0

Unicode字符集 (万国码)
UTF-8:使用1~4个字节保存

  • 英文字母:一个字节
  • 中文汉字:三个字节

img
注意:
UTF-8是一种编码格式

乱码问题

原因:

  1. 读取数据时未读完整个汉字
  2. 编码和解码时的方式不统一

如何不产生乱码?

  1. 不要用字节流读取文本文件
  2. 编码解码时使用同一个码表,同一个编码方式

字节流读取中文会乱码,但是为什么拷贝不会乱码呢?

编码和解码的具体实现

//1.编码
    String str ="ai你哟";
    byte[] bytes1 = str.getBytes();
    System.out.println(Arrays.toString(bytes1));//[97,105,-28,-67,-96,-27,-109,-97]

    byte[] bytes2 = str.getBytes("GBK");
    System.out.println(Arrays.toString(bytes2));//[97,105, -60,-29,-45,-76]


//2.解码
    String str2 = new String(bytes1);
    System.out.println(str2);

    String str3 = new String(bytes1,"GBK");
    System.out.println(str3);//乱码 ai三字不认识

解决乱码:字符流

FileReader

//1.创建字符输入流对象
    FileReader fr = new FileReader("myio\\a.txt");
//读取数据 
// public read()
//字符流的底层也是字节流,默认也是一个字节一个字节的读取的。
//如果遇到中文就会一次读取多个,GBK一次读两个字节,UTF-8一次读三个字节

//最终把十进制作为返回值
/* 
英文:文件里面二进制数据 0110 0091
    read方法进行读取,解码并转成十进制 97
中文:文件里面的二进制数据 1110011 10110001 10001001
    read方法进行读取,解码并转成十进制 27721
*/

    int ch;
    while((ch = fr.read()) != -1){
        System.out.print(char(ch));
        //输出汉字
    }

//3.释放资源
    fr.close();
//public read(char[] buffer)
//读取多个数据
//相当于空参的read + 强转类型转换
    FileReader fr = new FileReader( fileName: "myio\\a.txt");
    char[] chars = new char[2];
    int len;
    while((len = fr.read(chars)) != -1){
        //把数组中的数据变成字符串再进行打印
        System.out.print(new string(chars, 0, len));
        //ln删掉
    }

    fr.close();    

FileWriter

书写细节:

  1. 创建字符输出流对象:
    • 参数是字符串表示的路径或者File对象都是可以的
    • 如果文件不存在会创建一个新的文件,但是要保证父级路径是存在的细节
    • 如果文件已经存在,则会清空文件,如果不想清空可以打开续写开关
  2. 写数据:
    • 如果write方法的参数是整数,但是实际上写到本地文件中的是整数在字符集上对应的字符
  3. 释放资源
    • 每次使用完流之后都要释放资源
/*
void write(int c)  写出一个字符
void write(String str)  写出一个字符串
void write(string str, int off,int len)  写出一个字符串的一部分
void write(char[] cbuf)  写出一个字符数组
void write(char[] cbuf, int off, int len)  写出字符数组的一部分
*/
    FileWriter fw = new FileWriter("myio\\a.txt");
    fw.write(25185);

    fw.write("你好")
   
    char[] chars = {'a','b','c','我'};
    fw.write(chars);

    fw.close();
    
    FileWriter fw2 = new FileWriter("myio\\a.txt",true);//续写开关

字符流原理

输入流:
img
输出流:
img

练习1

拷贝文件夹

public class Test01 {
    public static void main(String[] args) throws IOException {
        //拷贝一个文件夹,考虑子文件夹

        //1.创建对象表示数据源
        File src = new File("D:\\aaa\\src");
        //2.创建对象表示目的地
        File dest = new File("D:\\aaa\\dest");

        //3.调用方法开始拷贝
        copydir(src,dest);



    }

    /*
    * 作用:拷贝文件夹
    * 参数一:数据源
    * 参数二:目的地
    *
    * */
    private static void copydir(File src, File dest) throws IOException {
        dest.mkdirs();
        //递归
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if(file.isFile()){
                //3.判断文件,拷贝
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));
                byte[] bytes = new byte[1024];
                int len;
                while((len = fis.read(bytes)) != -1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }else {
                //4.判断文件夹,递归
                copydir(file, new File(dest,file.getName()));
            }
        }
    }
}

文件加密:

public class Test02 {
    public static void main(String[] args) throws IOException {
        /*
            为了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
            加密原理:
                对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新的文件中。
            解密原理:
                读取加密之后的文件,按照加密的规则反向操作,变成原始文件。

             ^ : 异或
                 两边相同:false
                 两边不同:true

                 0:false
                 1:true

               100:1100100
               10: 1010

               1100100
             ^ 0001010
             __________
               1101110
             ^ 0001010
             __________
               1100100

        */
    }

    public static void encryptionAndReduction(File src, File dest) throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        int b;
        while ((b = fis.read()) != -1) {
            fos.write(b ^ 2);
        }
        //4.释放资源
        fos.close();
        fis.close();
    }


}

数字排序:

文本文件中有以下的数据:
2-1-9-4-7-8
将文件中的数据进行排序,变成以下的数据:
1-2-4-7-8-9

实现方式一:

public class Test03 {
    public static void main(String[] args) throws IOException {
        /*
            文本文件中有以下的数据:
                2-1-9-4-7-8
            将文件中的数据进行排序,变成以下的数据:
                1-2-4-7-8-9
        */


        //1.读取数据
        FileReader fr = new FileReader("myio\\a.txt");
        StringBuilder sb = new StringBuilder();
        int ch;
        while((ch = fr.read()) != -1){
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);
        //2.排序
        String str = sb.toString();
        String[] arrStr = str.split("-");//2-1-9-4-7-8

        ArrayList<Integer> list = new ArrayList<>();
        for (String s : arrStr) {
            int i = Integer.parseInt(s);
            list.add(i);
        }
        Collections.sort(list);
        System.out.println(list);
        //3.写出
        FileWriter fw = new FileWriter("myio\\a.txt");
        for (int i = 0; i < list.size(); i++) {
            if(i == list.size() - 1){
                fw.write(list.get(i) + "");
            }else{
                fw.write(list.get(i) + "-");
            }
        }
        fw.close();
    }
}

实现方式二:

public class Test04 {
    public static void main(String[] args) throws IOException {
        /*
            文本文件中有以下的数据:
                2-1-9-4-7-8
            将文件中的数据进行排序,变成以下的数据:
                1-2-4-7-8-9

           细节1:
                文件中的数据不要换行

            细节2:
                bom头
        */
        //1.读取数据
        FileReader fr = new FileReader("myio\\a.txt");
        StringBuilder sb = new StringBuilder();
        int ch;
        while((ch = fr.read()) != -1){
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);
        //2.排序
        Integer[] arr = Arrays.stream(sb.toString()
            .split("-"))
            .map(Integer::parseInt)
            .sorted()
            .toArray(Integer[]::new);
        //3.写出
        FileWriter fw = new FileWriter("myio\\a.txt");
        String s = Arrays.toString(arr).replace(", ","-");
        String result = s.substring(1, s.length() - 1);
        fw.write(result);
        fw.close();
    }
}

img

高级流

缓冲流

字节缓冲流

//1.创建缓冲流的对象
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myio\\a.txt"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myio\\copy.txt"));
//2.循环读取并写到目的地
//一次读取一个字节
    int b;
    while ((b = bis.read()) != -1){
        bos.write(b);
    }
//3.释放资源
    bos.close();
    bis.close();

//一次读取一个数组
    byte[] bytes = new byte[1024];
    int len;
    while((len = bis.read(bytes)) != -1){
        bos.write(bytes, 0, len);
    }

速度快的原因:

  • 缓冲流自带长度为8192的缓冲区
  • 可以显著提高字节流的读写性能
  • 对于字符流提升不明显,对于字符缓冲流而言关键点是两个特有的方法

字符缓冲流

原理:
底层自带了长度为8192的缓冲区提高性能

//字符缓冲输入流特有方法
    //public String readLine()
    //读取一行数据,如果没有数据可读了,会返回null


//1.创建字符缓冲输入流的对象
    BufferedReader br = new BufferedReader(new FileReader( fileName: "myio\\a.txt"));
//2.读取数据//细节:
//readLine方法在读取的时候,一次读一整行,遇到回车换行结束
//但是他不会把回车换行读到内存当中
    
    String line1 = br.readLine();
    System.out.println(line1);
    
    String line2 = br.readLine();
    System.out.printIn(line2);

    
//循环输出所有
    String line;
    while ((( line = br.readLine()) != null)){
        System.out.println(line);
    }

//3.释放资源
        br.close();
//字符缓冲输出流特有方法
    //public void newLine()
    //跨平台的换行

    BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt",true));
    //注意true写在FileWriter里,开启续写功能

    bw.write( str:"你嘴角上扬的样子,百度搜索不到");
    bw.newLine();
    bw.write( str:"以后如果我结婚了,你一定要来哦,没有新娘我会很尴尬");
    bw.newLine();

练习2

四种拷贝方式效率对比

/*
字节流的基本流:一次读写一个字节
字节流的基本流:一次读写一个字节数组
字节缓冲流:一次读写一个字节
字节缓冲流:一次读写一个字节数组
*/
//第2、4种时间较短

文本排序

案例分析

  1. 逐行读取文本信息。
  2. 把读取到的文本存储到集合中
  3. 对集合中的文本进行排序
  4. 遍历集合,按顺序,写出文本信息。
public class Demo05Test {
    public static void main(String[] args) throws IOException {
        //1.创建ArrayList集合,泛型使用String
        ArrayList<String> list = new ArrayList<>();
        //2.创建BufferedReader对象,构造方法中传递FileReader对象
        BufferedReader br = new BufferedReader(new FileReader("10_IO\\in.txt"));
        //3.创建BufferedWriter对象,构造方法中传递FileWriter对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("10_IO\\out.txt"));
        //4.使用BufferedReader对象中的方法readLine,以行的方式读取文本
        String line;
        while((line = br.readLine())!=null){
            //5.把读取到的文本存储到ArrayList集合中
            list.add(line);
        }
        //6.使用Collections集合工具类中的方法sort,对集合中的元素按照自定义规则排序
        Collections.sort(list, new Comparator<String>() {
            /*
                o1-o2:升序
                o2-o1:降序
             */
            @Override
            public int compare(String o1, String o2) {
                //依次比较集合中两个元素的首字母,升序排序
                return o1.charAt(0)-o2.charAt(0);
            }
        });
        //7.遍历ArrayList集合,获取每一个元素
        for (String s : list) {
            //8.使用BufferedWriter对象中的方法write,把遍历得到的元素写入到文本中(内存缓冲区中)
            bw.write(s);
            //9.写换行
            bw.newLine();
        }
        //10.释放资源
        bw.close();
        br.close();
    }
}

控制软件运行次数

img
IO流创建原则:

  • 随用随创建
  • 什么时候不用什么时候关闭

转换流

是字符流和字节流之间的桥梁
作用:

  • 字节流想要使用字符流中的方法
  • 指定字符集读写数据(JDK11之后已淘汰)
    public static void main(String[] args) throws IOException {

//利用转换流按照指定字符编码读取

    InputStreamReader isr = new InputStreamReader(new FileInputStream("myio\\gbkfile.txt"), "GBK");
//上面这个作为了解,被淘汰了


    FileReader fr = new FileReader("myio\\gbkfile.txt", Charset.forName("GBK"));
//掌握
        int ch;
    while ((ch = isr.read()) != -1){
        System.out.println((char)ch);
    }

     isr.close();
    }
//利用转换流按照指定字符编码写出

    
    FileWriter fw = new FileWriter("myio\\c.txt", Charset.forName("GBK"));
    fw.write( str:"你好你好");
    fw.close();

//将本地文件中的GBK文件,转成UTF-8
    FileReader fr = new FileReader("myio\\b.txt", Charset.forName("GBK"));
    FileWriter fw = new FileWriter("myio\\e.txt",Charset.forName("UTF-8"));
    int b;
    while ((b = fr.read()) != -1){
        fw.write(b);
    }
        fw.close();
        fr.close();
//利用字节流读取文件中的数据,每次读一整行,而且不能出现乱码

//1.字节流在读取中文的时候,是会出现乱码的,但是字符流可以搞定
//2.字节流里面是没有读一整行的方法的,只有字符缓冲流才能搞定

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("myio\\a.txt")));
    String line;
    while ((line = br.readLine()) != null){
        System.out.printIn(line);
    }
        br.close();

序列化流

可以把java中的对象写到本地文件中

//构造方法,把基本流包装成高级流
    public objectOutputStream(OutputStream obj)
//成员方法,把对象序列化到文件中
    public final void writeObject(object obj)


//1.创建对象
    Student stu = new Student("zhangsan", age: 23);
//2.创建序列化流的对象/对象操作输出流
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myio\\a.txt"));
//3.写出数据
    oos.writeObject(stu);
//4.释放资源
    oos.close();

//需要让Javabean实现标记型接口Serializable
//实现了这个接口,那么就表示当前的Student类可以被序列化

反序列化流

也叫对象操作输入流
可以把序列化到本地文件中的对象,读取到程序中来

//构造方法:把基本流变成高级流
    public ObjectInputStream(InputStream out)
//成员方法:把序列化到本地文件中的对象,读取到程序中来
    public Object readObject()

//1.创建反序列化流的对象
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myio\\a.txt"));
//2.读取数据
    Object o = ois.readObject();
//3.打印对象
    System.out.printIn(o);
//4.释放资源
    ois.close();

两者细节

//1.实现Serializable接口

//2.序列化流写到文件中的数据是不能修改的,一旦修改就无法再次读回来了

//3.序列化对象后,修改了Javabean类,再次反序列化,会抛出InvalidClassException异常
//解决方案:给Javabean类添加serialVersionUID (序列号、版本号)
//原因:文件中的版本号,跟Javabean的版本号不匹配

//4.如果一个对象中的某个成员变量的值不想被序列化,给该成员变量加transient关键字修饰

    public class Student implements Serializable{
        private static final long serialVersionUID = 1L;
    private String name;
    private int age;

//transient: 瞬态关键字
//作用:不会把当前属性序列化到本地文件当中
    private transient String address;

    private String address;
    }

//将多个自定义对象序列化到文件中,但是对象的个数不确定
//放到数组里
//序列化 
    ArrayList<Student> ist = new ArrayList<>();
    list.add(s1);
    list.add(s2);
    list.add(s3);

    0bjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myio\\a.txt"));
    oos.writeObject(list);
    oos.close();


//反序列化
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myio\\a.txt"));
//2.读取数据
    ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
    for(Student student : list){
        System.out.println(student);
    }
    ois.close();

打印流

分类:
打印流一般是指:PrintStream,PrintWriter两个类
特点:

  1. 打印流只操作文件目的地,不操作数据源
  2. 特有的写出方法可以实现,数据原样写出
    例如:打印:97 文件中:97
  3. 特有的写出方法特点3:可以实现自动刷新,自动换行
    打印一次数据 =写出+换行+刷新

字节打印流

img
img

    PrintStream ps = new PrintStream(new FileOutputStream("myio\\a.txt"), true, "UTF-8");

    ps.println(97);//自动换行
    ps.print(true);//不换行
    ps.printf("%s爱上了%s","阿珍","阿强");

    ps.close();

字符打印流

img
img

    PrintWriter pw = new PrintWriter(new FileWriter("myio\\a.txt"), true);

    pw.println("今天你终于叫我名字了,虽然叫错了,但是没关系,我马上改");
    pw.print("你好");

    pw.close();
//打印流的应用场景

//获取打印流的对象,此打印流在虚拟机启动的时候,由虚拟机创建,默认指向控制台
//特殊的打印流,系统中的标准输出流,是不能关闭,在系统中是唯一的。
    PrintStream ps = System.out;
//调用打印流中的方法println
//写出数据,自动换行,自动刷新
    ps.println("123");

解压缩流

//1.创建一个File表示要解压的压缩包
    File src = new File("D:\\a.zip");
//2.创建一个File表示解压的目的地
    File dest = new File("D:\\");

    public static void unzip(File src,File dest) throws IOException{

img

压缩流

//压缩单个文件

//1.创建File对象表示要压缩的文件
    File src = new File("D:\\a.txt");
//2.创建File对象表示压缩包的位置
    File dest = new File("D:\\");
//3.调用方法用来压缩
    toZip(src,dest);

img

//压缩整个文件夹

//1.创建File对象表示要压缩的文件夹
    File src = new File("D:\\aaa");
//2.创建File对象表示压缩包放在哪里(压缩包的父级路径)
    File destParent = src.getParentFile();//D:\\
//3.创建File对象表示压缩包的路径
    File dest = new File(destParent, src.getName() + ".zip");
//4.创建压缩流关联压缩包
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
//5.获取src里面的每一个文件,变成zipEntry对象,放入到压缩包当中
    toZip(src, zos, src.getName());//aaa
//6.释放资源
    zos.close();

img

Commons-io

Commons-io是apache开源基金组织提供的一组有关I0操作的开源工具包
作用:提高IO流的开发效率

//Commons-io 常见方法
//FileUtils类(文件/文件夹相关 )

    static void copyFile(File srcFile,File destFile)//复制文件

    static void copyDirectory(File srcDir,File destDir)//复制文件夹

    static void copyDirectoryToDirectory(File srcDir,File destDir)//复制文件夹

    static void deleteDirectory(File directory)//删除文件夹

    static void cleanDirectory(File directory)//清空文件夹

    static String readFileToString(File file,Charset encoding)//读取文件中的数据变成成字符串

    static void write(File file,CharSequence data,String encoding)//写出数据

//IOUtils类(流相关)

    public static int copy(InputStream input, OutputStream output)//复制文件

    public static int copyLarge(Reader input,Writer output)//复制大文件

    public static String readLines(Reader input)//读取数据

    public static void write(string data, OutputStream output)//写出数据
//示例

    File src = new File("myio\\a.txt");
    File dest = new File("myio\\copy.txt");
    FileUtils.copyFile(src,dest);

    File src = new File("D:\\aaa");
    File dest = new File("D: \\bbb");
    FileUtils.copyDirectoryToDirectory(src,dest);

    File src = new File("D:\\bbb");
    FileUtils.cleanDirectory(src);//清空

Hutool

难得糊涂 糊涂包

img

具体方法去官网上找

posted on   水吉z  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
 
点击右上角即可分享
微信分享提示