IO流的学习

1、文件基础知识

IO流的学习_第1张图片

什么是文件?

文件,就是保存数据的地方

IO流的学习_第2张图片

IO流的学习_第3张图片

package com.lhj.file;

import org.junit.Test;

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

public class FileCreate {

    public static void main(String[] args) {

    }

    // 方式一 直接写地址
    @Test
    public void createFile01() {
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 方式二 根据父目录文件 + 子路径 构建
    @Test
    public void create02() {
        File parentFile = new File("e:\\");
        String fileName = "news2.txt";
        // 这里的file对象 在java程序中 只是一个java对象
        // 只有执行了createNewFile方法 才会真正的在磁盘中创建该文件
        File file = new File(parentFile, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 方式三 根据父目录 + 子路径 创建
    @Test
    public void create03() {
        String parentPath = "e:\\";
        String fileName = "news3.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2、常用的文件操作

IO流的学习_第4张图片

package com.lhj.file;

import org.junit.Test;

import java.io.File;

public class FileInfomation {

    public static void main(String[] args) {

    }

    // 获取文件信息
    @Test
    public void info() {

        // 先创建文件对象
        File file = new File("e:\\news1.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.exists());
        // 是不是一个文件
        System.out.println("是不是一个文件=" + file.isFile());
        // 是不是一个目录
        System.out.println("是不是一个目录=" + file.isDirectory());
        
    }

}

目录操作和文件删除

mkdir创建一级目录 mkdirs创建多级目录 delete删除空目录或者文件

package com.lhj.file;

import org.junit.Test;

import java.io.File;

public class Directoty_ {

    public static void main(String[] args) {

    }

    // 判断 d:\\news1.txt 文件是否存在 如果存在则删除
    @Test
    public void deleteFile() {
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } else {
            System.out.println("文件不存在");
        }
    }

    // 判断 d:\\demo02 是否存在 存在就删除 否则提示不存在
    // 这里我们需要体会到 在java编程中 目录也被当做文件
    @Test
    public void deleteCatalogue() {
        String filePath = "e:\\demo2";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(filePath + "删除成功");
            } else {
                System.out.println(filePath + "删除失败");
            }
        } else {
            System.out.println("该目录不存在...");
        }
    }

    // 判断 e:\\demo\\a\\b\\c 目录是否存在 如果存在就提示已经存在 否则就创建
    @Test
    public void m3() {
        String filePath = "e:\\demo\\a\\b\\c";
        File file = new File(filePath);
        if (file.exists()) {
            System.out.println("该目录已经存在...");
        } else {
            if (file.mkdirs()) {
                System.out.println(filePath + "该目录创建成功...");
            } else {
                System.out.println(filePath + "该目录创建失败...");
            }
        }
    }


}

3、Java IO流原理

IO流的学习_第5张图片

IO流的学习_第6张图片

IO流的学习_第7张图片

InputStream和OutputStream这两个类都是抽象类,不能直接new出来,只可以使用其下的子类,Reader和Writer同样如此,均是抽象类!

IO流的学习_第8张图片

4、InputStream字节输入流

IO流的学习_第9张图片

4.1、FileInputStream

IO流的学习_第10张图片

IO流的学习_第11张图片

你可能感兴趣的:(学习)