java基础-IO流-day13

发布时间 2023-09-21 19:55:42作者: chenwen0511

1. IO的概念

计算机内存中的数据 <--> 硬盘里面的数据
也就是数据的落盘 以及 数据的读取

文件的操作

package com.msb.io01;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:32
 * @Description: com.msb.io01
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:"+ File.pathSeparator+"test.txt");
        System.out.println(f.canRead());
        System.out.println(f.canWrite());
        System.out.println(f.getName());
        System.out.println(f.isDirectory());
        System.out.println(f.isFile());
        System.out.println(f.isHidden());
        System.out.println(f.length());
        System.out.println(f.exists());

/*        if(f.exists()){
            f.delete();
        }else{
            f.createNewFile();
        }*/
        //路径相关
        System.out.println(f.getAbsolutePath());
        System.out.println(f.getPath());
        System.out.println(f.toString());

        System.out.println("--------------");
        File f5 = new File("demo.txt");
        if(!f5.exists()){
            f5.createNewFile();
        }
        File f6 = new File("a/b/c/demo.txt");
        if(!f6.exists()){
            f6.createNewFile();
        }
        System.out.println(f6.getAbsolutePath());//绝对路径
        System.out.println(f6.getPath());//相对路径
    }
}

目录的操作

package com.msb.io01;

import java.io.File;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:42
 * @Description: com.msb.io01
 * @version: 1.0
 */
public class Test02 {
    public static void main(String[] args) {
        File f = new File("C:\\Users\\chenw\\IdeaProjects\\Testuntitled");
        System.out.println("文件是否可读:"+f.canRead());
        System.out.println("文件是否可写:"+f.canWrite());
        System.out.println("文件的名字:"+f.getName());
        System.out.println("上级目录:"+f.getParent());
        System.out.println("是否是一个目录:"+f.isDirectory());
        System.out.println("是否是一个文件:"+f.isFile());
        System.out.println("是否隐藏:"+f.isHidden());
        System.out.println("文件的大小:"+f.length());
        System.out.println("是否存在:"+f.exists());
        System.out.println("绝对路径:"+f.getAbsolutePath());
        System.out.println("相对路径:"+f.getPath());
        System.out.println("toString:"+f.toString());

        String[] list = f.list();//字符串
        for (String s : list) {
            System.out.println(s);
        }
        
        File[] fs = f.listFiles();//file对象
        for (File file : fs) {
            System.out.println(file);
        }

    }
}

IO流的分类


2. 一个一个字符 完成文件的复制

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:53
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:/test.txt");
        FileReader fr = new FileReader(f);
        int n = fr.read();
//        while (n!=-1){
//            System.out.println(n);//单个字符的字节码打印
//            n = fr.read();
//        }
        while (n!=-1){
            System.out.println((char) n);//单个字符的打印
            n = fr.read();
        }

        fr.close();
    }
}

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:53
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:/test.txt");
        FileReader fr = new FileReader(f);

        // 缓冲数组
        char[] ch = new char[5];
        int len = fr.read(ch);//一次接收到的实际个数  不满足5的情况下

        while (len!=-1){
//            // 方式1 迭代输出
//            for (int i = 0; i < len; i++) {//根据实际长度迭代
//                System.out.println(ch[i]);
//            }
            //方式2 char列表转String
            String str = new String(ch, 0, len);
            System.out.print(str);

            len = fr.read(ch);
        }

        fr.close();
    }
}

输出内容到文件

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 21:06
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:demo.txt");

        FileWriter fw = new FileWriter(f);

        String str = "hello 美女";
        for (int i = 0; i < str.length(); i++) {
            fw.write(str.charAt(i));
        }



        fw.close();

    }
}

文件复制

package com.msb.io02;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 21:09
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo04 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:test.txt");
        File f2 = new File("d:demo.txt");


        FileReader fr = new FileReader(f1);
        FileWriter fw = new FileWriter(f2);

        // 方法一
//        int n = fr.read();
//        while (n!=-1){
//            fw.write(n);
//            n = fr.read();
//        }

        // 方法二
//        char[] ch = new char[5];
//        int len = fr.read(ch);
//        while (len!=-1){
//            fw.write(ch, 0, len);//实际有效长度
//            len = fr.read(ch);//注意这里也要传入ch
//        }
        // 方法三
        char[] ch = new char[5];
        int len = fr.read(ch);
        while (len!=-1){
            String str = new String(ch, 0, len);
            fw.write(str);
            len = fr.read(ch);
        }




        fw.close();
        fr.close();


    }
}

警告:不要用字符流去处理字节流
字符流:.txt .java .c .cpp 这些都是存文本的文件
字节流 非文本的文件:.jpg .mp3 .mp4 ...

3. 非文本 读取与复制