java IO流

本文详细介绍了Java中的文件操作,包括创建文件、遍历文件夹以及使用IO流进行文件读写。同时,讲解了字节流、字符流、缓冲流的使用,并展示了如何进行文件复制。此外,还涉及到了对象序列化和反序列化,以及Properties类在读取配置文件中的应用。
摘要由CSDN通过智能技术生成

File文件和文件夹

创建文件

 //.表示当前项目工作目录,分隔符Windows \ ;Linux / 所以用File.separator
 File file = new File("." + File.separator + "测试.txt");
  System.out.println("当前文件路径:" + file.getCanonicalPath());
  System.out.println("当前文件是否文件夹:" + file.isDirectory());
  //判断文件是否存在
  if (file.exists()) {
      System.out.println("文件已经存在");
  } else {
      //不存在创建文件
      file.createNewFile();
  }

遍历文件

File file = new File(".");
File[] files = file.listFiles();
if (files == null || files.length == 0) {
    System.out.println("此目录为空");
}
for (File f : files) {
    if (f.isDirectory()) {
        System.out.println("文件夹:" + f.getCanonicalPath());
    } else {
        System.out.println("文件:" + f.getCanonicalPath());
    }
}

IO流

1.数据存储的设备:文件File*;对象Object*
2.输入还是输出:读取(输入)、写出(输出)、复制(读写)【以程序为参照物】
3.是否要使用处理流:如果要提高读写效率或者要按行读取使用缓冲区,Buffered
4.Stream结束的是字节流,er结束的是字符流。Input开头是输入流,Output开头是输出流;
Buffered开始的表示缓冲流,Object开始的表示对象流。
5.字节流可以处理任意类型的数据,字符流可以处理纯文本,推荐文件使用字节流,纯文本用字符流处理。

字节输入流

在这里插入图片描述

字节输出流

在这里插入图片描述

字符输入流

在这里插入图片描述

字符输出流

在这里插入图片描述

使用字符流获取文本内容

File file = new File("." + File.separator + "测试.txt");
FileReader fr = new FileReader(file);
 char[] chars = new char[(int) file.length()];
 //所有内容都存放到字节数组btype中
 fr.read(chars);
 //由字节数组构造一个字符串
 String str = new String(chars);
 fr.close();
 return str;

使用字符流写入文本到文件中

File file = new File("."+File.separator+"文件.txt");
FileWriter fw = new FileWriter(file);
//true表示在源文件中追加
//FileWriter fw = new FileWriter(file,true);
//换行
//fw.write(System.lineSeparator());
fw.write("测试一下。");
fw.close();
Runtime.getRuntime().exec("notepad "+file.getCanonicalPath());

使用缓冲流复制文件

File file = new File("." + File.separator + "文件.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);

File file1 = new File("." + File.separator + "复制文件.txt");
FileOutputStream fos = new FileOutputStream(file1);
BufferedOutputStream bos = new BufferedOutputStream(fos);

//使用字节缓冲流复制文件,并不是缓冲区设置的越大效率越高
int b = -1;
while ((b = bis.read()) != -1) {
	bos.write(b);
}
bos.close();
fos.close();
bis.close();
fis.close();

对象序列化

1.对象序列化:将对象转换为二进制流的形式,便于网络传输
2.反序列化:将二进制的字节流转换为对象
注意:对象序列化,必须实现序列化接口implements Serializable
transient 关键字表示保护该字段,不序列化。

Student student = new Student("张三", "男");
//对象序列化,将数据转换为字节流写到本地磁盘
File file = new File("."+File.separator+"Student.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student);
oos.close();
fos.close();

//对象反序列化
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Student student1 = (Student) ois.readObject();
System.out.println(student1);
ois.close();
fis.close();

Properties类

用于读取properties的配置文件,配置文件常为.properties文件,是以键值对的形式进行参数配置的。

dbconfig.properties内容如下:

username = zs
password = 123
//方式一
Properties properties = new Properties();
File file = new File("." + File.separator + "dbconfig.properties");
FileInputStream fis = new FileInputStream(file);
properties.load(fis);
//方式二:通过类加载器获取
//properties.load(this.getClass().getClassLoader().getResourceAsStream("dbconfig.properties"));
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println(username+";"+password);

还有什么方法可以用请查看源码,哪里不会点哪里。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迷图羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值