【JAVA IO流】

IO导学

File类: 用于描述一个文件或者目录的。

通过File对象我们可以读取文件或者目录的属性数据,如果我们需要读取文件的内容数据,那么我们需要使用IO流技术。

IO流(Input Output)

IO流解决问题: 解决设备与设备之间的数据传输问题。 内存—>硬盘 硬盘—>内存

IO流技术:

IO流分类:
如果是按照数据的流向划分:

     输入流


     输出流

如果按照处理的单位划分:

     字节流: 字节流读取得都是文件中二进制数据,读取到的二进制数据不会经过任何的处理。


     字符流: 字符流读取的数据是以字符为单位的。 字符流也是读取文件中的二进制数据,不过会把这些二进制数据转换成我们能识别 的字符。
                字符流= 字节流 + 解码
如果按照处理的功能和位置划分:

     节点流:


     处理流:

输入字节流:
----------| InputStream 所有输入字节流的基类 抽象类
--------------| FileInputStream 读取文件数据的输入字节流

使用FileInputStream读取文件数据的步骤:
1. 找到目标文件
2. 建立数据的输入通道。
3. 读取文件中的数据。
4. 关闭 资源。

File类

方法描述
File对象.exists()获取文件或目录
File对象.getName()获取文件或目录的名字
File对象.createNewFile()创建一个新文件
File对象.mkdir()创建一个新的目录(但是不能递归创建)
File对象.mkdirs()创建递归目录
File对象.delete()删除文件或目录
File对象.deleteOnExit()文件或目录程序退出时删除
File对象.isDirectory()判断是否是目录
File对象.isFile()判断是否是文件
File对象.listFiles()获取指定目录的所有目录及文件
Arrays.asList()把某一个数组当作集合

File对象判断文件是否存在

使用file.exists()判断文件是否存在

public class FileTest {
    @Test
    public void test01(){
        // 根据路径创建File对象
        File file = new File("src/main/resources/笔记.txt");
        // 判断文件或目录是否存在
        boolean exists = file.exists();
        try {
            assert exists = true;
            System.out.println(file.getName()+"存在");
        }catch (AssertionError e){
            System.out.println(file.getName()+"不存在");
        }
    }
}

resources中有笔记.txt输出

笔记.txt存在

File对象创建目录或文件

File对象创建文件

使用file.createNewFile()创建一个新文件

public class FileTest {
@Test
    public void test02() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/a.txt");
        // 判断文件或目录是否存在
        if (!file.exists()){
            try {
                file.createNewFile();
                System.out.println("文件"+file.getName()+"创建成功");
            } catch (IOException e) {
                System.out.println("文件"+file.getName()+"创建失败");
                throw new RuntimeException(e);
            }
        }else {
            System.out.println(file.getName()+"已存在,终止创建过程!!!");
        }
    }
}

输出

文件a.txt创建成功

在这里插入图片描述

在这里插入图片描述

File对象创建目录

使用file.mkdir()创建目录 但不能递归创建

public class FileTest {
@Test
    public void test03() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/资料");
        // 判断文件或目录是否存在
        if (!file.exists()){
            // 创建目录 但不能递归创建
            if (file.mkdir()){
                System.out.println("目录"+file.getName()+"创建成功");
            }else {
                System.out.println("目录"+file.getName()+"创建失败");
            }
        }else {
            System.out.println(file.getName()+"已存在,终止创建过程!!!");
        }
    }
}

输出

目录资料3创建成功

递归创建目录

public class FileTest {
 @Test
    public void test04() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/资料/day33");
        // 判断文件或目录是否存在
        if (!file.exists()){
            // 递归创建
            if (file.mkdirs()){
                System.out.println("目录"+file.getName()+"创建成功");
            }else {
                System.out.println("目录"+file.getName()+"创建失败");
            }
        }else {
            System.out.println(file.getName()+"已存在,终止创建过程!!!");
        }
    }
}

输出

目录day33创建成功

File对象删除目录和文件

删除目录

public class FileTest {
 @Test
    public void test05() {
        // 根据路径创建File对象
        // File file = new File("src/main/resources/资料/day33");
         File file = new File("src/main/resources/资料");
        // 判断文件或目录是否存在
        if (file.exists()){
            // 删除目录
            if (file.delete()){
                System.out.println("目录" + file.getName()+"删除成功");
            }else {
                System.out.println("目录" + file.getName()+"删除失败");
            }
        }else {
            System.out.println(file.getName()+ "不存在!!!");
        }
    }
 }

输出

目录资料删除成功

删除文件

public class FileTest {
@Test
    public void test06() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/a.txt");
        // 判断文件或目录是否存在
        if (file.exists()){
            // 删除文件
            if (file.delete()){
                System.out.println("文件" + file.getName()+"删除成功");
            }else {
                System.out.println("文件" + file.getName()+"删除失败");
            }
        }else {
            System.out.println(file.getName()+ "不存在!!!");
        }
    }
}

输出

文件a.txt删除成功

File对象程序运行结束之后删除目录和文件

public class FileTest {
 @Test
    public void test07() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/a.txt");
        // 判断文件或目录是否存在
        if (file.exists()){
            // 程序运行结束之后删除文件或目录
            try {
                file.deleteOnExit();
                Thread.sleep(1000*10);
                System.out.println("文件" + file.getName()+"删除成功");
            } catch (InterruptedException e) {
               throw new RuntimeException(e);
            }
        }else {
            System.out.println(file.getName()+ "不存在!!!");
        }
    }
}

10秒之后删除输出

文件a.txt删除成功

File对象判断resources中的是目录还是文件

根据路径判断resources中的是目录还是文件

如:路径是src/main/resources/资料

public class FileTest {
 @Test
    public void test08() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/资料");
        // 判断文件或目录是否存在
        if (file.exists()){
            // 根据路径判断resources中的是目录还是文件
            System.out.println(file.isDirectory());
            System.out.println(file.isFile());
        }else {
            System.out.println(file.getName()+ "不存在!!!");
        }
    }
}

输出

true
false

如:路径是src/main/resources/笔记.txt

public class FileTest {
@Test
    public void test09() {
        // 根据路径创建File对象
        File file = new File("src/main/resources/笔记.txt");
        // 判断文件或目录是否存在
        if (file.exists()){
            // 根据路径判断resources中的是目录还是文件
            System.out.println(file.isDirectory());
            System.out.println(file.isFile());
        }else {
            System.out.println(file.getName()+ "不存在!!!");
        }
    }
}

输出

false
true

File对象遍历输出目录名和文件名

File对象遍历输出目录名和文件名

public class FileTest {
@Test
    public void test10() {
        // 根据路径创建File对象
        File file = new File("src/main/resources");
        // 获取所有目录和文件数组
        File[] files = file.listFiles();
        // 遍历输出目录名和文件名
        for (File newFile : files) {
            if (newFile.isDirectory()){
                System.out.println("目录 >>> "+newFile.getName());
            }else if (newFile.isFile()){
                System.out.println("文件 >>> "+newFile.getName());
            }
        }
    }
}

输出

文件 >>> 笔记.txt
目录 >>> 资料

File对象lambda表达式遍历输出目录名和文件名

public class FileTest {
 @Test
    public void test11() {
        // 根据路径创建File对象
        File file = new File("src/main/resources");
        // 获取所有目录和文件数组
        File[] files = file.listFiles();
        // lambda表达式遍历输出目录名和文件名
        List<File> fileList = Arrays.asList(files);
        fileList.forEach(newFile ->{
            if (newFile.isDirectory()){
                System.out.println("目录 >>> "+newFile.getName());
            }else if (newFile.isFile()){
                System.out.println("文件 >>> "+newFile.getName());
            }
        });
    }
}

输出

文件 >>> 笔记.txt
目录 >>> 资料

File对象StreamAPI过滤输出目录名

public class FileTest {
 @Test
    public void test12() {
        // 根据路径创建File对象
        File file = new File("src/main/resources");
        // 获取所有目录和文件数组
        File[] files = file.listFiles();
        // StreamAPI过滤输出目录名
        List<File> fileList = Arrays.asList(files);
        fileList.stream().filter(newFile -> newFile.isDirectory()).forEach(newFile -> System.out.println("目录 >>> "+newFile.getName()));
    }
}

输出

目录 >>> 资料

IO类(Input Output)

方法描述
File对象.length()获取在内存中的大小
inputStream.read()读取文件内容
outputStream.write()写入的字符串
outputStream.flush()刷新写入内容

File对象获取文本内容在内存中的存储大小

在resources里新建一个b.txt文件并输入内容
在这里插入图片描述获取b.txt文本内容在内存中的存储大小

public class Stream01Test {
    @Test
    public void test01(){
        File file = new File("src/main/resources/b.txt");
        // 获取在内存中的大小
        System.out.println(file.length());
    }
}

输出

3

FileInputStream读取文本内容

在resources里新建一个b.txt文件并输入内容
在这里插入图片描述

FileInputStream读取文本内容

public class Stream01Test {
 @Test
    public void test03(){
        // 声明文件位置
        File file = new File("src/main/resources/b.txt");
        // 声明字节输入流
        InputStream inputStream = null;
        try {
            // 实例化FileInputStream
            inputStream = new FileInputStream(file);
            // 使用read方法读取文件内容
            int read = inputStream.read();
            System.out.println((char) read);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

a

FileInputStream循环读取文本内容

public class Stream01Test {
@Test
    public void test04(){
        // 声明文件位置
        File file = new File("src/main/resources/b.txt");
        // 声明字节输入流
        InputStream inputStream = null;
        try {
            // 实例化FileInputStream
            inputStream = new FileInputStream(file);
            // 使用read方法读取文件内容
            int read = -1;
            while ((read=inputStream.read())!=-1){
                System.out.print((char) read);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

abc

FileInputStream利用数组循环读取文本内容

在resources里新建一个b.txt文件并输入内容
在这里插入图片描述
声明一个数组,使在读取字节的时候一次读取1024个字节

public class Stream01Test {
 @Test
    public void test08(){
        // 声明文件位置
        File file = new File("src/main/resources/b.txt");
        // 声明字节输入流
        InputStream inputStream = null;
        try {
            // 实例化FileInputStream
            inputStream = new FileInputStream(file);
            byte[] b = new byte[1024];
            // 使用read方法读取文件内容
            int read = -1;
            while ((read=inputStream.read(b))!=-1){
                System.out.print(new String(b,0,read));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

love爱love

FileOutputStream写入文件

把输出的内容变成getBytes数组
FileOutputStream目标文件后面加true就可以追加要写入的内容
在这里插入图片描述

public class Stream02Test {
    @Test
    public void tes01(){
        try {
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/c.txt");
            // 准备写入的字符串
            String content = "I love you";
            byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes);
            outputStream.flush();
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void tes02(){
        try {
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/c.txt",true);
            // 准备写入的字符串
            String content = "\nI love you too";
            byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes);
            outputStream.flush();
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void tes03(){
        try {
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/c.txt",true);
            // 准备写入的字符串
            String content = "\n桃李不言下自成蹊";
            byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
            outputStream.write(bytes);
            outputStream.flush();
            System.out.println("写入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出

写入成功

写入后的c.txt中的内容
在这里插入图片描述

字节输入输出流

字节输入输出流实现文件复制

public class Stream03Test {
    @Test
    public void test01(){
        try {
            // 原文件
            InputStream inputStream = new FileInputStream("src/main/resources/c.txt");
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/a.txt");
            int b = -1;
            while ((b = inputStream.read())!=-1){
                outputStream.write(b);
            }
            outputStream.flush();
            System.out.println("复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

复制完成

在这里插入图片描述

字节输入输出流复制二进制文件

public class Stream03Test {
@Test
    public void test02(){
        try {
            // 原文件
            InputStream inputStream = new FileInputStream("src/main/resources/9.jpg");
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/9.png");
            int b = -1;
            while ((b = inputStream.read())!=-1){
                outputStream.write(b);
            }
            outputStream.flush();
            System.out.println("复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

复制完成

在这里插入图片描述

字节输入输出流实现文件的加密与解密

加密
public class Stream03Test {
 @Test
    public void test03(){
        try {
            // 原文件
            InputStream inputStream = new FileInputStream("src/main/resources/9.jpg");
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/9.bmp");
            int b = -1;
            while ((b = inputStream.read())!=-1){
                outputStream.write(b ^ 123);
            }
            outputStream.flush();
            System.out.println("复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

复制完成

在这里插入图片描述

解密
public class Stream03Test {
@Test
    public void test04(){
        try {
            // 原文件
            InputStream inputStream = new FileInputStream("src/main/resources/9.bmp");
            // 目标文件
            OutputStream outputStream = new FileOutputStream("src/main/resources/99.bmp");
            int b = -1;
            while ((b = inputStream.read())!=-1){
                outputStream.write(b ^ 123);
            }
            outputStream.flush();
            System.out.println("复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

复制完成

在这里插入图片描述

字节输入输出流增加缓存复制文件

public class Stream03Test {
@Test
    public void test05(){
        try {
            // 原文件
            InputStream inputStream = new FileInputStream("pom.xml");
            // 目标文件
            OutputStream outputStream = new FileOutputStream("pom.txt");
            byte[] bytes = new byte[1024 * 8];
            int b = -1;
            while ((b = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,b);
            }
            outputStream.flush();
            System.out.println("复制完成");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

复制完成

在这里插入图片描述

带缓存的字节输入输出流

public class Stream04Test {
    @Test
    public void test01(){
        InputStream in = null;
        OutputStream out = null;
        InputStream input = null;
        OutputStream output = null;
        try {
            // 使用字节输入流读取文件
            in = new FileInputStream("pom.xml");
            // 使用字节输出流写入文件
            out = new FileOutputStream("pom.pom");
            // 使用带有缓存的字节输入流
            input = new BufferedInputStream(in);
            // 使用带有缓存的字节输出流
            output = new BufferedOutputStream(out);
            int b = -1;
            while ((b = input.read())!=-1){
                output.write(b);
            }
            output.flush();
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出

写入完成

在这里插入图片描述
BufferedInputStream缓存大小是8192
在这里插入图片描述
BufferedOutputStream缓存大小是8192
在这里插入图片描述

字符输入输出流

public class ReaderAndWriterTest {
    @Test
    public void test01(){
        Reader reader = null;
        try {
            reader = new FileReader("pom.xml");
            int read = -1;
            while ((read = reader.read())!=-1){
                System.out.print((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test02(){
        Writer writer = null;
        try {
            writer = new FileWriter("pom.pom");
            String content = "桃李不言下自成蹊";
            writer.write(content);
            writer.flush();
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test03(){
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("pom.xml");
            writer = new FileWriter("pom.pom");
            int read = -1;
            while ((read = reader.read())!=-1){
                writer.write(read);
            }
            writer.flush();
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test04(){
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("pom.xml");
            writer = new FileWriter("pom.pom");
            char[] chars = new char[1024 * 8];
            int read = -1;
            while ((read = reader.read(chars))!=-1){
                writer.write(chars,0,read);
            }
            writer.flush();
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test05(){
        Reader in = null;
        Writer out = null;
        Reader reader = null;
        Writer writer = null;
        try {
            in = new FileReader("pom.xml");
            out = new FileWriter("pom.pom");
            reader = new BufferedReader(in);
            writer = new BufferedWriter(out);
            int read = -1;
            while ((read = reader.read())!=-1){
                writer.write(read);
            }
            writer.flush();
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

输出

写入完成

字节流与字符相互转换

 @Test
    public void test06(){
        // 输入
        // 字节输入流
        InputStream in = null;
        // 输入处理流
        Reader input = null;
        // 缓存的字符输入流
        BufferedReader reader = null;

        // 输出
        // 字节输出流
        OutputStream out = null;
        // 输出处理流
        Writer output = null;
        // 缓存的字符输出流
        BufferedWriter writer = null;
        try {
            // 输入
            in = new FileInputStream("pom.xml");
            input = new InputStreamReader(in);
            reader = new BufferedReader(input);
            // 输出
            out = new FileOutputStream("pom.pom");
            output = new OutputStreamWriter(out);
            writer = new BufferedWriter(output);
            // 一次读取一行 但是读取不到文本分隔符 返回一个字符串
            String content = null;
            while ((content = reader.readLine())!= null){
                writer.write(content + "\n");
            }
            writer.flush();
            System.out.println("写入成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

输出

写入成功

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Rita_zzf

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

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

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

打赏作者

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

抵扣说明:

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

余额充值