0x04 IO流
文章目录
- 0x04 IO流
- 创建文件
- 读取文件信息
- 文件目录操作
- 删除
- 创建目录
 
- 文件流操作
- 读文件
- 写文件
- write(byte[] b) 方法
- write(byte[] b, int off, int len) 方法
 
- 文件拷贝
 
 
向大佬致敬: https://drun1baby.top
创建文件
三种方法 一张图对比着看

其实感觉差别不大,只是对File函数方法的传参值不同罢了
点进去就知道其实是多个带参构造方法


读取文件信息
package IOStream;
import java.io.File;
public class getFileInfo {
    public static void main(String[] args){
        getFileContents();
    }
    public static void getFileContents(){
        File file = new File("src/IOStream/CreateForFile/new1.txt");
        System.out.println("文件名称为:" + file.getName());
        System.out.println("文件的绝对路径为:" + file.getAbsolutePath());
        System.out.println("文件的父级目录为:" + file.getParent());
        System.out.println("文件的大小(字节)为:" + file.length());
        System.out.println("这是不是一个文件:" + file.isFile());
        System.out.println("这是不是一个目录:" + file.isDirectory());
    }
}

文件目录操作
删除
删除文件:

删除目录:

删除失败了 是因为该目录下有其他文件 无法删除,只能删除空目录
创一个测试一下

创建目录
区分一下两个函数就好

文件流操作
项目地址:F:\ourdemo\BliJavaSec\activeClass\src
前置:
按照操作数据单位不同分为:字节流和字符流
- 字节流(8bit,适用于二进制文件)
- 字符流(按字符,因编码不同而异,适用于文本文件)
读文件
- read()

很惭愧 没有解决中文输出乱码问题
不遗憾了 解决! ==> FileReader
private static void readFileWithChinese() { String filePath = "src/IOStream/CreateForFile/new1.txt"; FileReader fileReader = null; char[] cache = new char[1024]; //设置缓冲区 int readLen = 0; try { fileReader = new FileReader(filePath); while ((readLen = fileReader.read(cache)) != -1){ System.out.println(new String(cache, 0, readLen)); } }catch (IOException e){ e.printStackTrace(); }finally { try{ fileReader.close(); }catch (IOException e){ e.printStackTrace(); } } }
- read(byte[] d)

每次从缓存中读取 指定字节长度
private static void readFileWithCache() {
    String filePath = "src/IOStream/CreateForFile/new1.txt";
    FileInputStream fileInputStream = null;
    byte[] cache = new byte[8];   //设置缓冲区
    int readLen = 0;
    try {
        fileInputStream = new FileInputStream(filePath);
        while ((readLen = fileInputStream.read(cache)) != -1){
            System.out.println(new String(cache, 0, readLen));
        }
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try{
            fileInputStream.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
写文件
package IOStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFile {
    public static void main(String[] args){
        writeFile();
    }
    private static void writeFile() {
        String filePath = "src/IOStream/CreateForFile/new3.txt";
        FileOutputStream fileOutputStream = null;
        try{
            fileOutputStream = new FileOutputStream(filePath);
            String content = "asdasdasdasd";
            try{
                fileOutputStream.write(content.getBytes());
            }catch (IOException e){
                e.printStackTrace();
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }finally {
            try{
                fileOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

解释一下:
偏移量off : 从第几个字符开始输出
长度len : 需要输出长度

追加写


文件拷贝
其实简单来说就是先从一个文件中读出来 然后再写入到另一个文件中
相当于对上面两个的一个总结 自己写一下
package IOStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
    public static void main(String[] args){
        copyFile();
    }
    private static void copyFile() {
        String fromFilename = "src/IOStream/CreateForFile/new1.txt";
        String toFilename = "src/IOStream/CreateForFile/new3.txt";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        byte[] cache = new byte[1024];
        int readLen = 0;
        try{
            fileInputStream = new FileInputStream(fromFilename);
            fileOutputStream = new FileOutputStream(toFilename);
            while ((readLen = fileInputStream.read(cache)) != -1){
                fileOutputStream.write(cache, 0, readLen);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                fileInputStream.close();
                fileOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}




















