Java-IO流

常用的文件操作

image-20230829150940349

image-20230829151010566

public class FileCreate {
    public static void main(String[] args) {
    }
    //方式1 new File(String pathname)
    @Test
    public void create01() {
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式2 new File(File parent,String child) //根据父目录文件+子路径构建
    //e:\\news2.txt
    @Test
    public void create02() {
        File parentFile = new File("e:\\");
        String fileName = "news2.txt";
        //这里的file 对象,在java 程序中,只是一个对象
        //只有执行了createNewFile 方法,才会真正的,在磁盘创建该文件
        File file = new File(parentFile, fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //方式3 new File(String parent,String child) //根据父目录+子路径构建
    @Test
    public void create03() {
        //String parentPath = "e:\\";
        String parentPath = "e:\\";
        String fileName = "news4.txt";
        File file = new File(parentPath, fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //下面四个都是抽象类
    InputStream
    //OutputStream
    //Reader //字符输入流
    //Writer //字符输出流
}
  • 获取文件的相关信息

image-20230829151148077

image-20230829151153805

  • 目录的操作和文件删除

image-20230829151225802

流的分类

image-20230829151319534

  • IO 流体系图

image-20230829151333330

FileInputStream
/**
* 演示读取文件...
* 单个字节的读取,效率比较低
* -> 使用read(byte[] b)
*/
@Test
public void readFile01() {
    String filePath = "e:\\hello.txt";
    int readData = 0;
    FileInputStream fileInputStream = null;
    try {
        //创建FileInputStream 对象,用于读取文件
        fileInputStream = new FileInputStream(filePath);
        //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止。
        //如果返回-1 , 表示读取完毕
        while ((readData = fileInputStream.read()) != -1) {
            System.out.print((char)readData);//转成char 显示
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭文件流,释放资源.
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/**
* 使用read(byte[] b) 读取文件,提高效率
*/
@Test
public void readFile02() {
    String filePath = "e:\\hello.txt";
    //字节数组
    byte[] buf = new byte[8]; //一次读取8 个字节.
    int readLen = 0;
    FileInputStream fileInputStream = null;
    try {
        //创建FileInputStream 对象,用于读取文件
        fileInputStream = new FileInputStream(filePath);
        //从该输入流读取最多b.length 字节的数据到字节数组。此方法将阻塞,直到某些输入可用。
        //如果返回-1 , 表示读取完毕
        //如果读取正常, 返回实际读取的字节数
        while ((readLen = fileInputStream.read(buf)) != -1) {
            System.out.print(new String(buf, 0, readLen));//显示
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭文件流,释放资源.
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
FileOutputStream

image-20230829152619042

public void writeFile() {
    //创建FileOutputStream 对象
    String filePath = "e:\\a.txt";
    FileOutputStream fileOutputStream = null;
    try {
        //得到FileOutputStream 对象对象
        //老师说明
        //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容
        //2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面
        fileOutputStream = new FileOutputStream(filePath, true);
        //写入一个字节
        //fileOutputStream.write('H');//
        //写入字符串
        String str = "hsp,world!";
        //str.getBytes() 可以把字符串-> 字节数组
        //fileOutputStream.write(str.getBytes());
        /*
write(byte[] b, int off, int len) 将len 字节从位于偏移量off 的指定字节数组写入此文件输出流
*/
        fileOutputStream.write(str.getBytes(), 0, 3);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
FileReader 和 FileWriter

image-20230829152957157

  • FileReader 相关方法:

image-20230829153515354

  • FileWriter 常用方法

image-20230829153528713

节点流和处理流

image-20230829153657211

image-20230829153718787

  • 节点流和处理流的区别和联系

image-20230829153813306

处理流-BufferedReader 和BufferedWriter

image-20230829154104779

处理流-BufferedInputStream 和BufferedOutputStream

image-20230829154455855

image-20230829154635901

对象流-ObjectInputStream 和ObjectOutputStream
  • 功能:提供了对基本类型或对象类型的序列化和反序列化的方法
  • ObjectOutputStream 提供序列化功能
  • ObjectInputStream 提供反序列化功能

image-20230829154924383

转换流-InputStreamReader 和OutputStreamWriter

image-20230829155117608

打印流-PrintStream 和PrintWriter

image-20230829155246435

image-20230829155306912image-20230829155320211

Properties 类

image-20230829155504465

image-20230829155508167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值