Java基础——IO流

本文详细介绍了Java中的IO流,包括文件流的基本操作,如读写文件、获取文件信息和删除操作。接着,讨论了IO流的分类,如字节流与字符流,重点讲解了FileInputStream、FileOutputStream、FileReader和FileWriter的使用。还深入探讨了节点流与处理流的概念,以及如何利用BufferedReader和BufferedWriter提高性能。此外,还涉及了对象流的序列化和反序列化,标准输入输出流,转换流和打印流的应用。
摘要由CSDN通过智能技术生成

目录

一、文件流

1.1 常用的文件操作

 1.2 获取文件的相关信息

 1.3 目录的操作和文件删除

 二、IO 流的分类

2.1 流的分类

2.2 FileInputStream (字节输入流)

2.3 FileOutputStream 

2.3.1 实现在文件内写入

2.3.2 实现文件的拷贝

2.4 FileReader 和 FileWriter 

 2.4.1 FileReader 相关方法

 2.4.2 FileWriter 常用方法

2.5 节点流和处理流 

2.5.1 两者的区别

2.5.2 处理流的功能

2.5.3 处理流-BufferedReader 和 BufferedWriter

2.5.4  处理流-BufferedInputStream 和 BufferedOutputStream

2.5.5 对象流-ObjectInputStream 和 ObjectOutputStream

2.5.7 标准输入输出流

 2.5.8  转换流

2.5.9 打印流-PrintStream 和 PrintWriter



一、文件流

  1. 文件:保存数据的地方
  2. 文件流:文件在程序中是以流的形式来操作的。
  3. 流:数据在数据源(文件)和程序(内存)之间经历的路径

 输入流:数据从数据源(文件)到程序(内存)的路径

 输出流:数据从程序(内存)到数据源(文件)的路径

1.1 常用的文件操作

 

public class Yrh {
    public static void main(String[] args) {

    }

    @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();
        }
    }

    @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();
        }
    }

    @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();
        }
    }
}

 1.2 获取文件的相关信息

  1. 文件名字  getName()
  2. 文件绝对路径 getAbsolutePath
  3. 文件父级目录 getParent
  4. 文件大小(字节) length
  5. 文件是否存在 exists
  6. 是不是一个文件 isFile
  7. 是不是一个目录 isDirectory
    @Test
    public void info() {
        //先创建文件对象
        File file = new File("e:\\news1.txt");
        //调用相应的方法,得到对应信息
        System.out.println("文件名字=" + file.getName());
        //getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
        System.out.println("文件绝对路径=" + file.getAbsolutePath());
        System.out.println("文件父级目录=" + file.getParent());
        System.out.println("文件大小(字节)=" + file.length());
        System.out.println("文件是否存在=" + file.exists());//T
        System.out.println("是不是一个文件=" + file.isFile());//T
        System.out.println("是不是一个目录=" + file.isDirectory());//F
    }

文件名字=news1.txt
文件绝对路径=e:\news1.txt
文件父级目录=e:\
文件大小(字节)=0
文件是否存在=true
是不是一个文件=true
是不是一个目录=false

 1.3 目录的操作和文件删除

 二、IO 流的分类

2.1 流的分类

 字节流:二进制文件

字符流:文本文件

2.2 FileInputStream (字节输入流)

    @Test
    public void readFile01() {
        String filePath = "e:\\news2.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 {
        //关闭文件流,释放资源.
            t
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值