Java核心类库篇6——IO
1、File
1.1、构造方法
方法声明 | 功能介绍 |
---|---|
public File(File parent, String child) | 从父抽象路径名和子路径名字符串创建新的 File 实例 |
public File(String pathname) | 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例 |
public File(String parent, String child) | 从父路径名字符串和子路径名字符串创建新的File实例 |
public File(URI uri) | 通过将给定的file:URI转换为抽象路径名来创建新的File实例 |
1.2、方法
方法声明 | 功能介绍 |
---|---|
public boolean exists() | 测试此抽象路径名表示的文件或目录是否存在 |
public boolean exists() | 测试此抽象路径名表示的文件或目录是否存在 |
public long length() | 返回由此抽象路径名表示的文件的长度 |
public long lastModified() | 用于获取文件的最后一次修改时间 |
public String getAbsolutePath() | 用于获取绝对路径信息 |
public boolean delete() | 用于删除文件,当删除目录时要求是空目录 |
public boolean createNewFile() | 用于创建新的空文件 |
public boolean mkdir() | 用于创建目录 |
public boolean mkdirs() | 用于创建多级目录 |
public File[] listFiles() | 获取该目录下的所有内容 |
public boolean isFile() | 判断是否为文件 |
public boolean isDirectory() | 判断是否为目录 |
public File[] listFiles(FileFilter filter) | 获取目录下满足筛选器的所有内容 |
2、IO
IO就是Input和Output的简写,也就是输入和输出的含义
2.1、IO分类
- 按读写数据的基本单位
- 字节流:以字节为单位进行数据读写的流,可以读写任意类型的文件
- 字符流:以字符(2个字节)为单位进行数据读写的流,只能读写文本文件
- 按读写数据的方向不同
- 输入流:从文件中读取数据内容输入到程序中,也就是读文件
- 输出流:将程序中的数据内容输出到文件中,也就是写文件
- 按流的角色
- 节点流:直接和输入输出源对接的流
- 处理流:需要建立在节点流的基础之上的流
2.2、IO流的体系结构
红色为抽象类
蓝色为节点流,必须直接与指定的物理节点关联
分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
---|---|---|---|---|
抽象基类 | InputStream | OutputStream | Reader | Writer |
访问文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
访问数组 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
访问管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
访问字符串 | —— | —— | StringReader | StringWriter |
缓冲流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
转换流 | —— | —— | InputStreamReader | OutputStreamWriter |
对象流 | ObjectInputStream | ObjectOutputStream | —— | —— |
打印流 | —— | PrintStream | —— | PrintWriter |
FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
推回输入流 | PushbackInputStream | —— | PushbackReader | —— |
特殊流 | DataInputStream | DataOutputStream | —— | —— |
2.3、FileInputStream
逐个字节读
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
int l = 0;
while ((l=fileInputStream.read())!=-1){
System.out.println((char)l);
}
fileInputStream.close();
}
}
建立缓冲区读
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] b=new byte[1024];
int l = 0;
while ((l=fileInputStream.read(b))!=-1){
System.out.println(new String(b,0,l));
}
fileInputStream.close();
}
}
2.4、FileOutputStream
写入文件
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileOutputStream fileOutputStream = new FileOutputStream(file);
String str="hello world, i am ruoye!";
byte[] bytes = str.getBytes();
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}
追加内容
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
String str="hello world, i am ruoye!";
byte[] bytes = str.getBytes();
fileOutputStream.write(bytes);
fileOutputStream.close();
}
}
2.5、FileReader
逐个字符读
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileReader fileReader = new FileReader(file);
int l = 0;
while ((l=fileReader.read())!=-1){
System.out.println((char)l);
}
fileReader.close();
}
}
建立缓冲区读
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileReader fileReader = new FileReader(file);
char[] b=new char[1024];
int l = 0;
while ((l=fileReader.read(b))!=-1){
System.out.println(new String(b,0,l));
}
fileReader.close();
}
}
2.6、FileWriter
写入文件
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileWriter fileWriter = new FileWriter(file);
String str="你好,世界!";
fileWriter.write(str);
fileWriter.close();
}
}
追加内容
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileWriter fileWriter = new FileWriter(file,true);
String str="你好,世界!";
fileWriter.append(str);
fileWriter.close();
}
}
2.7、转换流
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File("D:\\a.txt")));
int l = 0;
while ((l=inputStreamReader.read())!=-1){
System.out.println((char)l);
}
inputStreamReader.close();
}
}
public class Test {
public static void main(String[] args) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File("D:\\a.txt")));
String str="hello wordld";
outputStreamWriter.write(str);
outputStreamWriter.close();
}
}
2.8、缓冲流
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes=new byte[1024];
int l = 0;
while ((l=fileInputStream.read(bytes))!=-1){
System.out.println(new String(bytes,0,l));
}
fileInputStream.close();
}
}
public class Test {
public static void main(String[] args) throws IOException {
String path="D:\\a.txt";
File file = new File(path);
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
String str="hello world, i am ruoye!";
byte[] bytes = str.getBytes();
bufferedOutputStream.write(bytes);
bufferedOutputStream.close();
fileOutputStream.close();
}
}
writer和reader同上
2.9、Data流
2.9.1、DataInputStream
- read(byte b[])—从数据输入流读取数据存储到字节数组b中
- read(byte b[],int off,in len)—从数据输入流中读取数据存储到数组b里面,位置从off开始,长度为len个字节
- readFully(byte b[])—从数据输入流中循环读取b.length个字节到数组b中
- readFully(byte b[],int off,in len )—从数据输入流中循环读取len个字节到字节数组b中.从b的off位置开始
- skipBytes(int b)—跳过n个字节
- readBoolean()—从数据输入流读取布尔类型的值
- readByte()—从数据输入流中读取一个字节
- readUnsignedByte()—从数据输入流中读取一个无符号的字节,返回值转换成int类型
- readShort()—从数据输入流读取一个short类型数据
- readUnsignedShort()—从数据输入流读取一个无符号的short类型数据
- readChar()—从数据输入流中读取一个字符数据
- readInt()—从数据输入流中读取一个int类型数据
- readLong()—从数据输入流中读取一个long类型的数据
- readFloat()—从数据输入流中读取一个float类型的数据
- readDouble()—从数据输入流中读取一个double类型的数据
- readUTF()—从数据输入流中读取用UTF-8格式编码的UniCode字符格式的字符串
public class Test {
public static void main(String[] args) throws IOException {
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("D:\\a.txt"));
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readInt());
System.out.println(dataInputStream.readBoolean());
System.out.println(dataInputStream.readShort());
System.out.println(dataInputStream.readLong());
System.out.println(dataInputStream.readDouble());
dataInputStream.close();
}
}
2.9.2、DataOutputStream
- intCount(int value)—数据输出流增加的字节数
- write(int b)—将int类型的b写到数据输出流中
- write(byte b[],int off, int len)—将字节数组b中off位置开始,len个长度写到数据输出流中
- flush()—刷新数据输出流
- writeBoolean()—将布尔类型的数据写到数据输出流中,底层是转化成一个字节写到基础输出流中
- writeByte(int v)—将一个字节写到数据输出流中(实际是基础输出流)
- writeShort(int v)—将一个short类型的数据写到数据输出流中,底层将v转换2个字节写到基础输出流中
- writeChar(int v)—将一个charl类型的数据写到数据输出流中,底层是将v转换成2个字节写到基础输出流中
- writeInt(int v)—将一个int类型的数据写到数据输出流中,底层将4个字节写到基础输出流中
- writeLong(long v)—将一个long类型的数据写到数据输出流中,底层将8个字节写到基础输出流中
- writeFloat(flloat v)—将一个float类型的数据写到数据输出流中,底层会将float转换成int类型,写到基础输出流中
- writeDouble(double v)—将一个double类型的数据写到数据输出流中,底层会将double转换成long类型,写到基础输出流中
- writeBytes(String s)—将字符串按照字节顺序写到基础输出流中
- writeChars(String s)—将字符串按照字符顺序写到基础输出流中
- writeUTF(String str)—以机器无关的方式使用utf-8编码方式将字符串写到基础输出流中
- size()—写到数据输出流中的字节数
public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("D:\\a.txt"));
dataOutputStream.writeUTF("α");
dataOutputStream.writeInt(1234567);
dataOutputStream.writeBoolean(true);
dataOutputStream.writeShort((short)123);
dataOutputStream.writeLong((long)456);
dataOutputStream.writeDouble(99.98);
dataOutputStream.close();
}
}
2.10、Zip流
压缩文件
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
File zip = new File("D:\\a.zip");
FileOutputStream fileOutputStream = new FileOutputStream(zip);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
int l = 0;
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
while ((l=fileInputStream.read())!=-1){
//压缩算法
zipOutputStream.write(l);
}
fileInputStream.close();
zipOutputStream.close();
fileOutputStream.close();
}
}
解压文件
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\a.txt");
File zip = new File("D:\\a.zip");
ZipFile zipFile= new ZipFile(zip);
ZipEntry entry = zipFile.getEntry("a.txt");
InputStream input = zipFile.getInputStream(entry);
FileOutputStream fileOutputStream = new FileOutputStream(file);
int l;
while ((l=input.read())!=-1){
//解压算法
fileOutputStream.write(l);
}
fileOutputStream.close();
input.close();
}
}
压缩多个文件
待补
解压多个文件
待补
2.11、Object流
想要使用ObjectOutputStream和ObjectInputStream,对象一定要序列化
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明
private transient String name;
2.11.1、ObjectOutputStream
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("D:\\a.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
objectOutputStream.writeObject(new Person("zhangsan",20));
objectOutputStream.close();
}
}
2.11.2、ObjectInputStream
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("D:\\a.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
Person person = (Person) objectInputStream.readObject();
System.out.println(person);
}
}
2.12、RandomAccessFile
方法声明 | 功能介绍 |
---|---|
public RandomAccessFile(String name, String mode) | 根据参数指定的名称和模式构造对象 r: 以只读方式打开 rw:打开以便读取和写入 rwd:打开以便读取和写入,同步文件内容的更新 rws:打开以便读取和写入,同步文件内容和元数据 的更新 |
int read() | 读取单个字节的数据 |
void seek(long pos) | 用于设置从此文件的开头开始测量的文件指针偏移量 |
void write(int b) | 将参数指定的单个字节写入 |
void close() | 用于关闭流并释放有关的资源 |
public class Test {
public static void main(String[] args) throws IOException {
RandomAccessFile r = new RandomAccessFile("D:\\a.txt", "rw");
r.seek(1);
System.out.println((char) r.read());
r.write('b');
r.close();
}
}