1、I/O流(常用16个)
文件输入和输出
读文件【输入】要关闭:close();
写文件【输出】要刷新:flush();
文件专属 | 转换流 | 缓冲流 | 标准输出流 | 数据专属流 | 对象专属流 |
---|---|---|---|---|---|
FileInputStream | InputStreamReader | BufferInputStream | DataInputStream | ObjectInputStream | |
FileOutputStream | OutputStreamWriter | BufferOutputStream | PrintStream | DataOutputStream | ObjectOutputStream |
FileReader | BufferReader | ||||
FileWriter | BufferWriter | PrintWriter |
FileInputStream方法
read();读取一个字节
read(byte数组对象);将数据读到byte数组中,返回读取到的个数,读完返回-1
available();还剩几个字节没有读,返回未读的数量,可以获得文件全部的字节数量
skip(4);跳过四个字节
FileInputStream fis = null;
try{
fis = new FileInputStream("文件路径");
/*
//read()方法,一次读一个字节,读一个自动后移一位,读完返回-1【不常用】
int readDate = 0;
while((readDate = fis.read()) != -1){
System.out.print(readDate);
}
*/
byte[] bytes = new byte[10];
int readCount = 0;
//read(byte数组对象);方法返回读到的字节个数,读完返回-1
while((readCount = fis.read(bytes)) !=-1){
System.out.print(new String(bytes,0,readCount));
}
}catch(){}
finally(fis != null){
try{
fils.close();
}catch(){}
}
FileOutputStream方法
write(byte数组);
write(byte数组,起始下标,个数);
FileOutputStream fos = null;
try{
//没有文件会创建,有文件会清空文件中的内容
fos = new FileOutputStream("文件路径");
//不删除文件中内容,追加的方式写入
//fos = new FileOutputStream("文件路径",true);
byte[] bytes = [10,11,12,13];
fos.write(bytes);
//fos.write(bytes,0,2);
//将数据刷入文件
fos.flush();
}catch(){}
finally(fis != null){
try{
fils.close();
}catch(){}
}
BufferReader方法
自带缓冲区
readLine();读一行,返回这一行字符串,读完返回一个null【不带换行符,需要使用println换行】
//FileReader作为节点流
FileReader reader = new FileReader("文件地址");
//构造方法需要传递一个FileReader对象
//BufferReader作为包装流
BufferReader br = new BufferReader(reader);
String s = null;
if((s = br.readLine()) != null){
System.out.println(s);
}
//关闭时只需要关闭包装流,里面的节点流会自动关闭
br.close();
DataOutputStream数据专属流
会将数据和数据的类型一并写入文件,这个文件不是普通的文本文档
只能使用DataInputStream读,并且读的顺序和写的顺序要一致
标准输出流
PrintStream:不需要关闭,会自动关闭
//联合写
System.out.println("xxx");
//分开写
PrintStream ps = new System.out;
ps.println("xxx");
//改变输入方向
//将要输出的数据通过文件输出流输出到文件中,不再输出到控制台【打印日志文件的框架的原理】
PrintStream pr = new PrintStream(new FileOutStream("log",true));
System.setOut(pr);
对象专属流(序列化和反序列化)
序列化版本号:如果没写,系统会默认提供,可以用来区分 类。
【不同的人编写的同样的类,但这两个类确实不是同一个类】
【一个类,生成序列化版本号之后,在更改部分代码后会生成全新的序列化版本号】
【同一个类,更改代码后被认为是不同的类,所以建议手动写序列化版本号】
//自定义序列化版本号
private static final long serialVersion UID = 1L;
ObjectOutputStream:序列化【Serialize】
将java对象切成一块一块的,编上号,存到文件当中
同时序列化多个对象:
将多个对象实现Serialize接口,放入集合中,然后序列化这个集合
用transient修饰的属性不参与序列化操作,反序列化时,被设为初始值
Student st = new Student(20,"xxx");
//对象序列化存储位置
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("xxx"));
//序列化对象
oos.writeObject(st);
//对象需要实现Serializable接口,使对象变成可序列化的
public void Studendt implements Seralizable{
//用transient修饰的属性不参与序列化操作
private transient int age;
private String name;
public Student(int age,String name){
this.age = age;
this.name = name;
}
setxxx
getxxx
toStringxxx
}
ObjectInputStream:反序列化【DeSerialize】
将硬盘中的数据恢复到内存中,恢复组装成java对象
//获得序列化文件
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("xxx"));
//反序列化,读出对象
Object obj = ois.read();
2、File类
文件和目录路径名的抽象表示形式,不能进行数据读写
方法
FIle f = new File("xxx");//创建一个File对象
//判断文件是否存在
if(f.exists()){
f.creatNewFile();//以文件形式新建
f.mkdir();//以目录形式新建
f.mkdirs();//以多重目录形式新建
}
String ff = f.getParent();//获取文件的父路径
File fff = f.getParentFile();
f.getAbsolutePath();//获取绝对路径
f.getName();//获取文件名
f.isFile();//判断是否是文件
f.isDirectory();//判断是否是路径
long haomiao = f.lastModified();//获取文件最后一次修改的时间【从1970年1月1日至今的毫秒】
f.length();//获取文件大小
File[] files = f.listFiles();//或取当前文件下的所有子文件
File和IO流组合可以进行文件复制