Java学习笔记之IO流

IO流

1 流的分类

  • 按照操作数据单位分类:字节流(二进制文件)、字符流(文本文件)
  • 按照数据流的流向不同:输入流、输出流
  • 按照流的角色不同:节点流、处理流/包装流
抽象基类 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer

2 文件流

2.1 定义
  • 输入流:数据从数据源(文件)到程序(内存)之间的路径
  • 输出流:数据从程序(内存)到数据源(文件)之间的路径
2.2 创建文件
public class CreateFile {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\16671\\Desktop\\new01.md";
        String parentPath = "C:\\Users\\16671\\Desktop\\";
        String child1 = "new02.md";
        String child2 = "new03.md";
		
        CreateFile createFile = new CreateFile();
        //方式一
        createFile.create01(filePath);
        //方式二
        File parentFile = new File(parentPath);
        createFile.create02(parentFile, child1);
        //方式三
        createFile.create03(parentPath, child2);
    }
    
    //方式一 new File(String filePath)     绝对路径
    public void create01(String filePath){
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("Success 01");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式二 new File(File parent, String child) 	父目录文件 + 子目录
    public void create02(File parent, String child){
        File file = new File(parent, child);
        try {
            file.createNewFile();
            System.out.println("Success 02");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //方式三 new File(String parentPath, String childPath)    父目录 + 子目录
    public void create03(String parentPath, String childPath){
        File file = new File(parentPath, childPath);
        try {
            file.createNewFile();
            System.out.println("Success 03");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
2.3 获取文件信息
public class FileInfo {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\16671\\Desktop\\new03.md";
        File file = new File(filePath);
		
        //获取文件绝对路径
        System.out.println("getAbsolutePath : " + file.getAbsolutePath());
        //文件是否存在
        System.out.println("exists : " + file.exists());
        //获取文件名
        System.out.println("getName : " + file.getName());
        //获取文件父路径
        System.out.println("getParent : " + file.getParent());
        //获取文件路径
        System.out.println("getPath : " + file.getPath());
        //是否是文件
        System.out.println("isFile : " + file.isFile());
        //是否是路径
        System.out.println("isDictionary : " + file.isDirectory());
        //获取文件大小
        System.out.println("fileLength : " + file.length());
    }
}
2.4 文件及目录的创建和删除
public class DictionaryControl {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\16671\\Desktop\\new01.md";
        String path1 = "C:\\Users\\16671\\Desktop\\dir";
        String path2 = "C:\\Users\\16671\\Desktop\\dir2\\demo\\a";
        DictionaryControl dictionaryControl = new DictionaryControl();
        dictionaryControl.dir01(filePath);
        dictionaryControl.dir02(path1);
        dictionaryControl.dir03(path2);
    }
    
    //判断文件是否存在,存在就删除
    public void dir01(String filePath){
        File file = new File(filePath);
        if (file.exists()){
            if (file.delete())
                System.out.println("删除成功");
            else System.out.println("删除失败");
        }else {
            System.out.println("文件不存在");
        }
    }

    //判断目录是否存在,存在则删除该目录(Java 中,目录也被视作文件)
    public void dir02(String path){
        File file = new File(path);
        if (file.exists()){
            if (file.delete())
                System.out.println("删除成功");
            else System.out.println("删除失败");
        }else {
            System.out.println("目录不存在");
        }
    }

    //判断目录是否存在,不存在就创建它
    //mkdirs() 创建多级目录       mkdir() 创建一级目录
    public void dir03(String path){
        File file = new File(path);
        if (file.exists()){
            System.out.println("目录已经存在");
        }else{
            if (file.mkdirs())
                System.out.println("目录创建成功");
            else System.out.println("目录创建失败");
        }
    }
}

3 InputStream 常用子类

3.1 子类关系图

InputStream子类关系图

3.2 FileInputStream
//read()
public class InStreamInfo {
    @Test
    public void readFile01(){
        String filePath = "e:\\TestFile\\hello.txt";
        int readData = 0;
        try {
            //从输入流读取一个字节的数据,如果可读,返回一个 int 型
            //若返回 -1,代表读取到了文件末尾
            FileInputStream fileInputStream = new FileInputStream(filePath);
            while ((readData = fileInputStream.read()) != -1){
                System.out.print((char)readData);
            }
            //一定要关闭文件流,释放资源占用
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//read(byte[] bytes)
public class InStreamInfo {
    @Test
    public void readFile02(){
        String filePath = "e:\\TestFile\\hello.txt";
        int readLen = 0;
        byte[] buf = new byte[8];   //每次读取八个字节
        try {
            //从输入流读取指定长度的字节,读取成功返回读取的总字节个数
            //若返回 -1,代表读取到了文件末尾
            FileInputStream fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(buf)) != -1){
                System.out.print(new String(buf, 0, readLen));
            }
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
3.3 BufferedInputStream
3.4 ObjectInputStream

4 OutputStream 常用子类

4.1 子类关系图

OutputStream子类关系图

4.2 FileOutputStream
public class OutStreamInfo {
    @Test
    public void writeFile(){
        String filePath = "e:\\TestFile\\hello.txt";
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            String originStr = "Hello, Java!";
            //单个字符写入    write()
            fileOutputStream.write('H');                
            //写入一个指定长度的 byte[] 数组   write(byte[] bytes)
            fileOutputStream.write(originStr.getBytes());
            //写入 byte[] 数组的指定长度     write(byte[] bytes, int off, int len)
            fileOutputStream.write(originStr.getBytes(), 0, 5);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
posted @   悟道九霄  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示