IO流
概念:程序与文件之间存储数据的通道
流的划分:
1. 按方向划分: 输入流,输出流
输入流:将文件中的数据读到程序中
输出流:将程序的数据写到文件中
2. 按单位划分: 字节流,字符流
字节流:读写最小单位,按一个一个字节读写,效率低;读写所有文件(音频,视频,图片)
字符流:按一个一个字符读写,效率高;只能读写文本文件
3. 按功能划分:节点流,处理流
节点流:基础流,没有经过处理的流
处理流:经过处理包装后的流,功能更为强大
1.1 字节节点流
======================字节节点流======================
字节流的抽象父类: OutputStream,InputStream
子类:
字节流的节点流: FileOutputStream,FileInputStream
字节节点流的读取:FileInputStream
细节: 实例化输出流,会自动创建文件; 实例化输入流,则不会自动创建
=========字节流的图片拷贝=============
//案例: 实现图片的拷贝,将b.jpg拷贝到d盘,并改为a.jpg----d:\\a.jpg
//分析:循环地通过输入流先读取b.jpg,然后写到指定路径的输出流中
public class Test3 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("b.jpg");
FileOutputStream fos = new FileOutputStream("d:\\a.jpg");
byte[] b = new byte[1024];
int len;
while((len=fis.read(b))!=-1) { //每次读1024个长度,放到字节数组
fos.write(b,0,len); //3个参数,写数据是完整的
}
IOUtils.closeAll(fis,fos); //统一资源关闭
}
}
1.2 字节缓冲流
------字节缓冲输出流-----
字节缓冲流:BufferedOutputStream/BufferedInputStream
带缓冲区的字节流--处理流(包装之后得到的流),减少与磁盘交互次数,提升了性能
带两个参数的缓冲区对象,第二个参数是缓冲区大小,如果不指定默认为8192(1024*8,恒定值)
越接近恒定值,缓冲区存储性能越好
1.3字节流-对象流
对象流: ObjectOutputStream/ObjectInputStream
1. 也是一个处理流,是一个带缓冲区的流
2. 增加了存储对象的功能
===============对象流存单个对象=============
//案例: 对象流存储自定义对象
//存储自定义对象的类,必须实现序列化,才可通过对象流存储
//Serializable:标记型接口,无需实现重写方法
class Person implements Serializable{
private String name;
public Person(String name) {
this.name=name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
public class Test1 {
@Test //单元测试,只需在成员方法中+@Test
public void writeTest() throws FileNotFoundException, IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
//oos.writeObject(new String("zsf")); //存字符串
oos.writeObject(new Person("zsf")); //存自定义对象-序列化
oos.close();
}
@Test
public void readTest() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
//String person = (String)ois.readObject();
Person person = (Person) ois.readObject(); //反序列化
System.out.println(person);
ois.close();
}
}
===========对象流存多个对象============
1.在对象流中可以设置某些属性不参与序列化--transient,static
2.读写多个对象的问题
方式1:捕获异常
方式2:使用集合存储
细节:字节节点流可以往文件追加内容,所以,处理流,字节缓冲流,对象流都可以追加内容
追加内容往往只用在存日志中(且更多的是字符流存日志-效率更高)
2. 字符流
字符流: 通过一个字符一个字符读写,效率比字节流高,但只能读写文本文件
抽象父类: Writer/Reader
2.1. 字符节点流
字符节点流:FileWriter/FileReader(富二代流)
特点:可以追加存储,及简化代码量
@Test
public void writeTest() throws IOException {
FileWriter fw = new FileWriter("a.txt",true); //参数2:追加
fw.write("hello,world");
fw.close();
}
@Test
public void readTest() throws IOException {
FileReader fr = new FileReader("a.txt");
char[] cbuf = new char[1024];
int len=fr.read(cbuf);
System.out.println(new String(cbuf,0,len));
fr.close();
}
2.2 字符缓冲流(重点)
字符缓冲流: BufferedWriter/BufferedReader
带缓冲的字符流,读写效率非常高
提供了两个非常常用的方法,读写换行:newLine/readLine
@Test
public void writeTest() throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
for(int i=0;i<5;i++) {
bw.write("好好学习..."+i);
bw.newLine(); //写内容换行
}
bw.close();
}
@Test
public void readTest() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String msg;
while((msg=br.readLine())!=null) {
System.out.println("打印:"+msg);
}
br.close();
}
2.3 打印输出流
PrintWriter: 输出字符流 , 输出内容到文件
具有print/println等方法,用于打印换行
支持原样打印
PrintWriter pw = new PrintWriter("a.txt");
pw.println(66);
pw.println(3.14);
pw.close();
//PrintStream:输出字节流 系统调的,输出到系统的控制台
//PrintWriter与PrintStream都是打印输出流,只不过一个是字节流,一个是字符流
System.out.println(66);
System.out.println(3.14);
2.4 字符转换流
字符转换流: OutputStreamWriter/InputStreamReader
该类直接从字节流转换到字符流的
转换流可以进行乱码处理: 用什么编码写,那么就用什么编码读,才不会出现乱码
@Test
public void writeTest() throws IOException {
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"));
//字符转换流,可以进行读写时编码设置
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),"GBK");
osw.write("hello,io流");
osw.close();
}
@Test
public void readTest() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"GBK");
char[] cbuf = new char[1024];
int len = isr.read(cbuf);
System.out.println(new String(cbuf,0,len));
isr.close();
}
3.1 File类常用方法
File类的常用方法:
只需要指定好File对象的路径,则可知道该对象的信息
File file = new File("E:\\Users\\Administrator\\eclipse-workspace\\Day22_CharIO\\b.txt");
System.out.println("是否为文件:" + file.isFile());
System.out.println("是否为目录:" + file.isDirectory()); // 目录就是文件夹
System.out.println("是否为可读:" + file.canRead());
System.out.println("是否为可写:" + file.canWrite());
System.out.println("是否为隐藏:" + file.isHidden());
System.out.println("获取绝对路径:" + file.getAbsolutePath());
System.out.println("获取相对路径:" + file.getPath());
System.out.println("获取文件名:" + file.getName());
System.out.println("获取父级目录:" + file.getParentFile());
System.out.println("获取文件长度:" + file.length());
System.out.println("文件是否存在:" + file.exists());
System.out.println("最后一次修改时间:" + file.lastModified());
Date date = new Date(file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
//System.out.println("重命名:" + file.renameTo(new File("b.txt")));
System.out.println("删除文件或删除空目录:"+file.delete());
3.2 创建不存在父级目录的文件
File类的应用:
创建指定目录下的文件; 如果上一级目录不存在,则先创建上一级目录
分析:先获取父级目录,如果父级目录不存在,则创建; 然后再创建子文件
File file = new File("a/b/a.txt");
//获取父级目录
File parent = file.getParentFile();
if(parent!=null&&!parent.exists()) {
if(parent.mkdirs()) {
System.out.println("创建父级目录成立");
}
}
if(!file.exists()){
if(file.createNewFile()) {
System.out.println("创建文件成功");
}
}
3.4 递归操作文件
扩展:如果需要找多层的txt
递归,往往就用在File中
概述: 自己调自己,且需要退出的出口;
回顾说明: 如果能用其他方式,尽量不要用递归;两个弊端:1.效率低 2.容易死递归
public static void main(String[] args) {
f(new File("a"));
}
private static void f(File file) {
File[] files = file.listFiles(); //获取指定目录的所有文件,存到File数组中
//遍历数组
if(files!=null) {
for(File f:files) {
if(f.isFile()&&f.getName().endsWith(".txt")){
System.out.println(f);
}else if(f.isDirectory()) {
f(f); //是目录,则递归调自己 //a/b
}
}
}
}
3.5FileFilter
FileFilter:文件过滤器
listFiles方法用于将当前层的文件和目录的File对象放入数组
FileFilter则是在放入过程中的过滤
//案例:在指定的a目录下,查找当前层txt为后缀的文件
//分析:1.后缀为txt;2.是文件
//具体操作:实例化a目录的对象;取出下面的文件和目录;再进行判断
public class Test1 {
public static void main(String[] args) {
File file = new File("a");
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//如果为true,则放行;false就拦截
return pathname.isFile()&&pathname.getName().endsWith(".txt");
}
});
System.out.println(Arrays.toString(files));
}
}
4.Properties集合
Properties:Map的子类,用于存储键值对
存储键值对都是字符串类型
与IO流有关,可以将文件的内容加载的Properties;也可以将Properties内容存储到文件
Properties properties = new Properties();
properties.put("aaa", "12344");
properties.put("bbb", "65555");
properties.put("ccc", "76666");
//存储
properties.store(new FileOutputStream("a.txt"), "store");
Properties p = new Properties();
//加载文件的键值对
p.load(new FileInputStream("a.txt"));
//p.getProperty(key):根据key,获取value,都是字符串类型
System.out.println(p);