File类的使用
File构造器
构造器文件路径参数:
File类的获取功能:
调用listFiles的话输出的是绝对路径
renameTo重命名
File的判断功能
File类的创建和删除功能:
IO流原理及流的分类
根据数据单位不同可以分为:字节流(8bit),字符流(16bit): 文本数据使用字符流,非文本数据使用字节流 根据流向不同分为:输入流,输出流 根据流的角色分为:节点流,处理流
IO流体系结构
节点流(文件流)
FileInputStream
read(byte[] buffer)
FileOutputStream
write(byte[] buffer, 0, len)
FileReader
read() read(char[] ch) read(char[] ch,int offset, int length)
// File类的实例化
File file = new File("hello.txt");
// FileReader流的实例化
FileReader fr = new FileReader(file);
// 读入的操作
/// 1. fr.read() 每次读一个字符,直到返回-1
int data;
while(data = fr.read())!=-1){
System.out.print((char)data);
}
/// 2. int fr.read(char[] ch) 每次读ch数组长度的字符,直到返回-1
char[] cbuf = new char[5];
int len;
while((len=fr.read(cbuf))!=-1){
for(int i = 0;i<len;i++)
{
System.out.print(cbuf[i]);
}
// 除了for循环也可以用String构造器来实现
String str = new String(cbuf,0,len);
System.out.print(str);
}
// 资源的关闭
fr.close();
FileWriter
输出的文件是可以不存在的 如果文件之前存在,可以对之前文件进行覆盖或者追加,具体看append参数(默认是false)。 FileWrite(file,true) 不会覆盖 FileWrite(file,false) 会覆盖 write(String str) write(char[] ch,0.len)
缓冲流(处理流的一种)
提供流的读取、写入的速度 加快了速度的原因:底层提供了一个
BufferedInputStream
read(byte[] buffer)
BufferedOutputStream
write(byte[] buffer,0,len)
BufferedReader
br.read() br.read(char[] ch) br.readLine() // 一次读一行
BufferedWriter
bw.newLine(); // 相当于增加了一个换行符 bw.write(char[] ch,0,len)
转换流
用于字符流和字节流之间的转换:
InputStreamReader
可以将一个字节的输入流转换为字符的输入流 FileInputStream fis = new FileInputStream("hell.txt"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); // 读入一个UTF-8的文件
OutputStreamWriter
将一个字符的输出 流转换为字节的输出流 FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt"); InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
// 读写过程 char[] cbuf = new char[30]; int len; while((len = isr.read(cbuf))!=-1){ osw.write(cbuf, 0.len); }
打印流
标准的输入输出流:
-
System.in:标准的输入流,默认从键盘输入 System.out:标准的输出流,默认从控制台输出
-
打印流:PrintStream 和PrintWriter 2.1 提供了重载的
将标准的输出流改成文件:
public void test(){
PrintStream ps = null;
try{
FileOutputStream fos = new FileOutputStream(new File("D\\IO\\text.txt"));
ps = new PrintStream(fos,true);
if(ps != null){
System.setOut(ps); // 设置输出终端
for (int i = 0; i<=255; i++){
System.out.print((char) i);
if (i%50 ==0){
System.ot.println();
}
}
}
}
数据流(处理流的一种)
DateInputStream
DataInputStream dis = new DataInputStream(new FileInputStream("hell.txt")); String str = dis.readUTF(); int age = dis.readInt();
DataOutputStream
DataOutputStream dos = new DataOutputStream(new FileOutputStream("hell.txt")); dos.writeUTF("mts"); dos.flush(); // 每执行一次就会将内存中的数据写入文件 dos.writeInt(23); dos.writeBoolean(true); dos.close();
对象流(序列化机制,处理流的一种)
对象的序列化
将内存中的java对象保存到存盘中或通过网络传输出去: ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oos.dat");
oos.writeObject(new String("我爱天安门“)); oos.flush(); // 刷新写入 oos.close();
对象的反序列化
反序列化:将磁盘文件中的对象还原为内存中的java对象 ObjectInputStrea ois = new ObjectInputStream(new FileInputStream("oos.dat"); Object obj = ois.readObject(); String str = (String) obj; ois.close();
自定义对象序列化
Serializable为标识接口,内部没有抽象方法。 如果想要一个java对象是可序列化的,需要满足以下要求:
- 实现Serializable接口
- 需要当前类提供一个全局常量:serializableUID
- 除了当前Person类需要实现Serializable接口值钱了,还必须保证其内部所有属性也必须是可序列化的。
补充:ObjectInputStream和ObjectOutputStream不可以修饰static和transient修饰的成员变量。
public static final long serializableUID = 22313214124L
(RandomAccessFile)随机存取文件流
RandomAccessFile直接继承于Object类,实现了DataInput和DataOutput接口 RandomAccessFile既可以作为一个输入流,又可以作为一个输出流 如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建,如果写出的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从头覆盖)
常用方法:
seek(int index); // 将指针调到角标为index的位置 write() read()
NIO.2中Path、Pahts、Files类的使用