Java中File、字节流

File案例

需求:从控制台输入一个字符串 该字符串是文件夹路径 计算这个文件夹的大小

public static File getDirFile() {
    Scanner scanner = new Scanner(System.in);
    while(true) {
        System.out.println("请输入一个文件夹路径:");
        String path = scanner.nextLine();
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("你输入的路径不存在,请重新输入");
        }else if (file.isFile()) {
            System.out.println("你输入的是个文件,请重新输入");
        }else {
            return file;
        }
    }
}

计算文件夹的大小

public static long getDirLengthByFile(File file) {
    long sum = 0;
    File[] listFiles = file.listFiles();
    for (File subF : listFiles) {
        if (subF.isFile()) {
            sum = sum + subF.length();
        }else {
            sum = sum + getDirLengthByFile(subF);
        }           
    }
    return sum;
}
/*
 * 测试
 */
public static void fun() {
    File file = getDirFile();
    long length = getDirLengthByFile(file);
    System.out.println(length);
}

需求:从控制台输入一个字符串 该字符串是文件夹路径 删除这个文件夹

/*
 * 根据文件夹路径删除这个文件夹
 */
public static void deleteDir(File file) {
    File[] listFiles = file.listFiles();
    for (File subFile : listFiles) {
        if (subFile.isFile()) {
            subFile.delete();
        }else {
            deleteDir(subFile);
        }           
    }
    file.delete();
}
/*
 * 测试
 */
public static void fun() {
    File file = getDirFile();
    deleteDir(file);
}
字节流
参照物:该执行程序。
输出(OutPut)定义:程序到文件的输出过程
输入(InPut)定义:文件到程序的输入过程

OutputStream(字节输出流):

是个抽象类 是所有输出流的父类

OutputStream的方法:

/*
 * 三种写入方法
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileOutputStream oStream = new FileOutputStream(file);
    //  写入数据
    oStream.write(49);
    //  写入字节数组
    byte[] b = {65, 66, 67, 68};
    oStream.write(b);
    //  字节数组的索引和长度写入
    oStream.write(b, 1, 2);
    oStream.close();
}
/*
 * 文件的续写和换行
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileOutputStream oStream = new FileOutputStream(file, true);
    oStream.write("holle\n".getBytes());
    oStream.close();
}
/*
 * 异常处理(手动)
 */
public static void fun() {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FlieOutPutStream oStream = null;
    try {
        oStream = new  FlieOutPutStream(file);
        oStream.write("holle".getBytes());
    } catch (FileNotFoundException e) {
        throw new RuntimeException("文件找不到");
    } catch (IOException e) {
        throw new RuntimeException("文件编写失败");
    } finally {
        try {
            if (oStream != null) {
                oStream.close();
            }
        } catch (IOException e) {
            throw new RuntimeException("关闭失败");
        }
    }
}
InputStream(字节输入流):

1.是所有输入流的父类
2.注意:字节流写入的是一个字节一个字节写,读取也是一个字节一个字节的读
InputStream的方法:
/*
 * 读取方法
 * 注意:当读取到文件末尾时 会返回-1(相当于读取结束)
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    int i1 = iStream.read();
    System.out.println((char)i1);       
    i1 = iStream.read();
    System.out.println((char)i1);       
    i1 = iStream.read();
    System.out.println(i1);     
    i1 = iStream.read();
    System.out.println(i1);
    iStream.close();
}
/*
 * 循环读
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    int read = 0;
    while((read = iStream.read()) != -1) {
        System.out.println((char)read);
    }
    iStream.close();
}
/*
 * 使用字节数组 读取文件
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    byte[] b = new byte[1024];
    int i1 = iStream.read(b);
    System.out.println(i1); // 2
    System.out.println(new String(b));// ab
    iStream.close();
}










展开阅读全文

没有更多推荐了,返回首页