[Java基础] IO流(十五)

本文详细介绍了Java中的文件操作,包括创建、获取信息、目录操作和文件删除,以及IO流原理、分类和使用实例,如FileInputStream、FileOutputStream、FileReader和FileWriter的常见方法。

文件

文件是保存数据的地方

文件流

文件在程序中是以流的形式来操作的

  • 流:数据在数据源(文件)和程序(内存)之间经历的路径
  • 输入流:数据从数据源(文件)到程序(内存)的路径
  • 输出流:数据从程序(内存)到数据流(文件)的路径

文件操作

创建文件

  • 1⃣️new File(String pathname) 根据路径创建一个File对象
  • 2⃣️new File(File parent,String child)根据父目录文件 + 子路径构建
  • 3⃣️new File(String parent,String child)根据父目录 + 子路径构建

获取文件信息

  • getName()-------文件名称
  • getAbsolutePath-()文件绝对路径
  • getParent()------文件父目录
  • length()----------文件大小(字节)
  • exists()----------文件是否存在
  • isFile()-----------是不是文件
  • isDirectory()-----是不是路径

目录操作和文件删除

  • mkdir:创建以及目录
  • mkdirs:创建多级目录
  • delete:删除空目录和文件

IO流原理

  • I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理数据传输,如读/写文件,网络通讯等
  • Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行
  • java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据
  • 输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中
  • 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

流的分类

  • 按操作数据单位不同分为:
    1⃣️字节流(8 bit)二进制文件2⃣️字符流(按字符)文本文件
  • 按数据的流行不同分为:
    1⃣️输入流2⃣️输出流
  • 按流的角色的不同分为:
    1⃣️节点流2⃣️处理流/包装流
抽象基类字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter
  • Java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的
  • 由这四个类派生出来的子类名称都是以其父类名作为子类名后缀

IO流体系图

在这里插入图片描述

文件 VS 流

FileInputStream

package Java.File;

import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * @author 鹿青舟
 * @version 1.0
 */
public class FileInputStream {
    public static void main(String[] args) {

    }
    @Test
    public void read1() {
        String filePath = "/Users/zhouqinglu/Documents/JAVA/new1.txt";
        int readData = 0;
        java.io.FileInputStream fileInputStream = null;
        try {
            fileInputStream = new java.io.FileInputStream(filePath);

            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char) readData);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }    @Test
    public void read2() {
        String filePath = "/Users/zhouqinglu/Documents/JAVA/new1.txt";
        int readLen = 0;
        byte buf[] = new byte[8];
        java.io.FileInputStream fileInputStream = null;
        try {
            fileInputStream = new java.io.FileInputStream(filePath);

            //.read(buf)返回实际读取的字节数
            while ((readLen = fileInputStream.read(buf)) != -1) {
//                System.out.println(readLen);
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

FileOutputStream

package Java.File;

import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
 * @author 鹿青舟
 * @version 1.0
 */
public class FileOutputStream {
    public static void main(String[] args) {

    }

    @Test
    public void write1() {
        String filePath = "/Users/zhouqinglu/Documents/JAVA/hello.txt";
        java.io.FileOutputStream fileOutputStream = null;
        try {
//            fileOutputStream = new java.io.FileOutputStream(filePath);
            fileOutputStream = new java.io.FileOutputStream(filePath,true);
            fileOutputStream.write('H');
            String str = "hello,world";
            fileOutputStream.write(str.getBytes());
//            fileOutputStream.write(str.getBytes(),0,5);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}

FileReader

相关方法

  • new FileReader
  • read:每次读取单个字符,返回该字符,如果到文件末尾返回-1
  • read(char[]):批量读取多个字符到数组,返回读取的字符数,如果到文件末尾返回-1
  • new String(char[]):将char[]转换成String
  • new String(char[], off, len):将char[]的指定部分准还成String
package Java.File;

import java.io.IOException;

/**
 * @author 鹿青舟
 * @version 1.0
 */
public class FileReader {
    public static void main(String[] args) {
        String filePath = "/Users/zhouqinglu/Documents/JAVA/new2.txt";
        java.io.FileReader fileReader = null;
//        int data = 0;
        int readLen = 0;
        char buf[] = new char[8];
        try {
            fileReader = new java.io.FileReader(filePath);
//            while ((data = fileReader.read()) != -1) {
//                System.out.print((char) data);
//            }
            while ((readLen = fileReader.read(buf)) != -1) {
                System.out.print(new String(buf,0,readLen));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

FileWriter

常用方法

  • new FileWriter(File/String)
  • new FileWriter(File/String, true)
  • write(int)
  • write
package Java.File;

import java.io.IOException;

/**
 * @author 鹿青舟
 * @version 1.0
 */
public class FileWriter {
    public static void main(String[] args) {
        String filePath = "/Users/zhouqinglu/Documents/JAVA/new3.txt";
        java.io.FileWriter fileWriter = null;
        char chars[] = {'a', 'b', 'c'};
        try {
//            fileWriter = new java.io.FileWriter(filePath);
            fileWriter = new java.io.FileWriter(filePath,true);
            fileWriter.write('H');
            fileWriter.write(chars);
            fileWriter.write(" 输入输出".toCharArray(),0,3);
            fileWriter.write(" 明天头秃");
            fileWriter.write(" 明天头秃",0,3);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileWriter.close();
//                fileWriter.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.println("写入成功");
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你怎么知道我头发乌黑浓密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值