【JavaSec】JavaIO流

0x04 IO流


向大佬致敬: https://drun1baby.top

创建文件

三种方法 一张图对比着看

image-20240819141758128

其实感觉差别不大,只是对File函数方法的传参值不同罢了

点进去就知道其实是多个带参构造方法

image-20240819141954386

image-20240819141958681

读取文件信息

package IOStream;

import java.io.File;

public class getFileInfo {
    public static void main(String[] args){
        getFileContents();
    }

    public static void getFileContents(){
        File file = new File("src/IOStream/CreateForFile/new1.txt");
        System.out.println("文件名称为:" + file.getName());
        System.out.println("文件的绝对路径为:" + file.getAbsolutePath());
        System.out.println("文件的父级目录为:" + file.getParent());
        System.out.println("文件的大小(字节)为:" + file.length());
        System.out.println("这是不是一个文件:" + file.isFile());
        System.out.println("这是不是一个目录:" + file.isDirectory());
    }
}

image-20240819142931863

文件目录操作

删除

删除文件:

image-20240819143614920

删除目录:

image-20240819143902026

删除失败了 是因为该目录下有其他文件 无法删除,只能删除空目录

创一个测试一下

image-20240819144037576

创建目录

区分一下两个函数就好

image-20240819144807868

文件流操作

项目地址:F:\ourdemo\BliJavaSec\activeClass\src

前置:

按照操作数据单位不同分为:字节流字符流

  • 字节流(8bit,适用于二进制文件)
  • 字符流(按字符,因编码不同而异,适用于文本文件)

读文件

  • read()

image-20240819152242787

很惭愧 没有解决中文输出乱码问题

不遗憾了 解决! ==> FileReader

private static void readFileWithChinese() {
String filePath = "src/IOStream/CreateForFile/new1.txt";
FileReader fileReader = null;
char[] cache = new char[1024];   //设置缓冲区
int readLen = 0;
try {
  fileReader = new FileReader(filePath);
  while ((readLen = fileReader.read(cache)) != -1){
      System.out.println(new String(cache, 0, readLen));
  }
}catch (IOException e){
  e.printStackTrace();
}finally {
  try{
      fileReader.close();
  }catch (IOException e){
      e.printStackTrace();
  }
}

}

image-20240819164245074

  • read(byte[] d)

image-20240819153316490

每次从缓存中读取 指定字节长度

private static void readFileWithCache() {
    String filePath = "src/IOStream/CreateForFile/new1.txt";
    FileInputStream fileInputStream = null;
    byte[] cache = new byte[8];   //设置缓冲区
    int readLen = 0;
    try {
        fileInputStream = new FileInputStream(filePath);
        while ((readLen = fileInputStream.read(cache)) != -1){
            System.out.println(new String(cache, 0, readLen));
        }
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try{
            fileInputStream.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

写文件

  • write(byte[] b) 方法
package IOStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFile {
    public static void main(String[] args){
        writeFile();
    }

    private static void writeFile() {
        String filePath = "src/IOStream/CreateForFile/new3.txt";
        FileOutputStream fileOutputStream = null;
        try{
            fileOutputStream = new FileOutputStream(filePath);
            String content = "asdasdasdasd";
            try{
                fileOutputStream.write(content.getBytes());
            }catch (IOException e){
                e.printStackTrace();
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }finally {
            try{
                fileOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}

image-20240819155608222

  • write(byte[] b, int off, int len) 方法

解释一下:

偏移量off : 从第几个字符开始输出

长度len : 需要输出长度

image-20240819160438400

追加写

image-20240819160522875

image-20240819160602588

文件拷贝

其实简单来说就是先从一个文件中读出来 然后再写入到另一个文件中

相当于对上面两个的一个总结 自己写一下

package IOStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args){
        copyFile();
    }

    private static void copyFile() {
        String fromFilename = "src/IOStream/CreateForFile/new1.txt";
        String toFilename = "src/IOStream/CreateForFile/new3.txt";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        byte[] cache = new byte[1024];
        int readLen = 0;
        try{
            fileInputStream = new FileInputStream(fromFilename);
            fileOutputStream = new FileOutputStream(toFilename);

            while ((readLen = fileInputStream.read(cache)) != -1){
                fileOutputStream.write(cache, 0, readLen);
            }

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                fileInputStream.close();
                fileOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }

    }
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值