java File类 和 IO流

File类

文件和文件夹(文件路径)的抽象表示,是专门来出来磁盘上面的文件或文件夹的

构造方法

方法

返回boolean

creatNewFile()

生成一个文件,当且仅当具有该名称的文件尚不存在时

public class Demo02 {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\2.txt");
        // 生成一个2.txt文件
        System.out.println(file.createNewFile());
    }
}
mkdir() / mkdirs()

生成目录

public class Demo02 {

    public static void main(String[] args) throws IOException {
        File file1 = new File("C:\\Users\\Air\\Desktop\\test\\aa");
       // 生成目录   生成一个aa目录
        System.out.println(file1.mkdir());
    }
}
delete() 删除空文件和目录
isFile() 判断是否是文件
isDirectory() 判断是否是目录
isHidden() 判断是否是隐藏文件
isAbsoute() 判断是否是绝对路径
exists() 判断文件或者文件夹是否存在

返回String

getName() 获取文件或目录的名字
getPath() 获取当前对象的路径的
getParent() 获取当前文件对象的上一级目录

IO流

文件上传和下载

缓冲概念

计算机通过CPU内存读取磁盘上面的文件数据,一次读取1字节。但是可以加上缓冲的概念,每次读取4kb。效率会高点

IO流分类 : 输入流 和 输出流

输入流:从磁盘读取到内存中

输出流:从内存写入到磁盘中

输入流

输入流分为字节输入流 和 字符输入流

字节输入流【重要】

磁盘上有一个1.txt文件,现在要将1.txt文件中的内容读取到内存中(java代码中)

FileInputStream 文件输入流 (字节输入流)

BufferedInputStream() 缓冲流

public class Demo {
    public static void main(String[] args) throws IOException {
        // 找到 文件
        File file = new File("C:\\Users\\Air\\Desktop\\test\\1.txt");
        // 将我们找到的文件转为字节输入流的形式  ,之后可以按照流的形式写入到内存中  FileInputStream 不具备缓冲效果
        FileInputStream fis = new FileInputStream(file);
        // 加上缓冲效果
        // BufferedInputStream为另一个输入流添加了功能,即缓冲输入和支持mark和reset方法的功能。 当创建BufferedInputStream时,将创建一个内部缓冲区数组。
        BufferedInputStream bis = new BufferedInputStream(fis);
    }
}
public class Demo02 {
    public static void main(String[] args) throws IOException {
        // 创建File对象
        File file = new File("C:\\Users\\Air\\Desktop\\test\\1.txt");
        // 创建字节流对象
        FileInputStream fileInputStream = new FileInputStream(file);
        // 缓冲
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        // 声明一个缓冲数组
        byte[] bytes = new byte[1024 * 4];

        // 开始使用 read 读取
        int length ;
        while ((length = bis.read(bytes))!= -1) {
            // 打印
            System.out.println(new String(bytes,0,length));
        }
        // 关闭流
        bis.close();
        fileInputStream.close();
    }
}
字符输入流 【不经常使用】

也是输入流,将磁盘的一个文件读取到内存(java代码中)

FileReader() 文件输入流 (字符输入流)

BufferedReader() 缓冲流

是一个阅读字符文件的便利类,是专门处理字符文件的,比如txt文件 。音视频图片不能使用这个流

public class Demo06 {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\11.txt");
        // 新建一个字符流对象
        FileReader fr = new FileReader(file);
        // 添加缓冲效果
        BufferedReader br = new BufferedReader(fr);
        // 添加字符数组
        char[] chars = new char[4 * 1024];

        int length = -1;

        while ((length = br.read(chars)) != -1) {
            System.out.println(new String(chars,0,length));
        }
        br.close();
        fr.close();
    }
}

输出流

字节输出流【重要】

将java中的数据写入到磁盘中(文件中)

写入用write() 写完后一定要刷新流然后关闭流 先开的后关 后开的先关 顺序不能乱

FileOutputStream 文件输入流

BufferedOutputStream 缓冲

import java.io.*;

public class Demo {
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Air\\Desktop\\test\\2.txt");
        // 实例化字节流输出对象
        FileOutputStream fos = new FileOutputStream(file);
        // 添加缓冲效果
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 将字符串写入文件中
        String str = "hello world";
        // 将字符串转为字节数组
        byte[] bytes = str.getBytes();
        bos.write(bytes);
        // 写完后一定要刷新流然后关闭流
        bos.flush();   // 没有实际意义,可以不写
        //关闭流
        // 先开的后关 后开的先关 顺序不能乱
        bos.close();
        fos.close();
    }
}

字符输出流 【不经常使用】

将java中的数据写入到磁盘中(文件中)

FileWriter 字符输出流

public class Demo07 {
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\Users\\Air\\Desktop\\test\\22.txt");
        // 创建字符输出流对象
        FileWriter fw = new FileWriter(file);
        // 缓冲效果
        BufferedWriter bw = new BufferedWriter(fw);

        String str = "hello world 你好 世界";

        // 将字符串转为 chat类型数组 toCharArray
        char[] chars = str.toCharArray();

        bw.write(chars);

        // 关闭流
        bw.close();
        fw.close();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不是很菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值