Java教程

java IO流

本文主要是介绍java IO流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println(username+";"+password);

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

这篇关于java IO流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!