前言
文件的输入输出流(IO流)学习笔记
文件对象
字符串
文件路径
文件夹路径
不存在路径
创建对象
- 不管文件是否存在,都会新建空文件
- 如果目录不存在,会出异常
一个参数
<directory>
:目录名
<file>
:文件名
1
| File file = new File("<directory>/<file>");
|
两个参数
1
| File file = new File("<directory>", "<file>");
|
方法
文件或文件夹属性
获取文件大小
获取文件名
获取父目录
获取最后修改时间
判断是否是文件
判断是否是目录
判断是否存在
新建文件
- 如果文件已存在,则不新建,返回false
- 如果父目录不存在,会出现异常
新建目录
创建多层目录
删除文件或空目录
目录列表
- 对文件、不存在的路径、无权进入的目录,列表方法返回null
IO流
输入流,只能用来读取数据
输出流,只能用来输出数据
字节流
字节读取流(输入流)
- 父类为抽象类,不能创建对象
- 字节流可以处理任何数据的文件
- 子类继承父类,可以创建对象
- 是一个字节一个字节的读取
创建对象
<str>
:字符串类型的文件路径
1
| InputStream in = new FileInputStream(<str>);
|
<file>
:封装文件路径的File对象
1
| InputStream fin = new FileInputStream(<file>);
|
读取
1 2 3 4 5 6 7 8 9 10 11
| public static void FIS() throws IOException { InputStream fin = new FileInputStream("/Users/hatsunemiku/Downloads/1.txt"); int b = 0; while ((b=fin.read()) != -1) { System.out.println(b); } fin.close(); }
|
- 子类继承父类,可以创建对象
- 是一个数组一个数组的读取
- 缓冲数组长度默认为8192
创建对象
- 可以将任何InputStream子类作为对象传入参数
1 2
| InputStream fin = new FileInputStream(<file>); InputStream bis = new BufferedInputStream(fin);
|
<len>
:直接给定数组长度
1 2
| InputStream fin = new FileInputStream(<file>); InputStream bis = new BufferedInputStream(fin, <len>);
|
读取
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void BIS() throws IOException { InputStream fin = new FileInputStream("/Users/hatsunemiku/Downloads/1.txt"); InputStream bis = new BufferedInputStream(fin); int b = 0; while ((b=bis.read()) != -1) { System.out.println(b); } fin.close(); bis.close(); }
|
字节写出流(输出流)
OutputStream
- 父类为抽象类,不能创建对象
- 字节流可以处理任何数据的文件
FileOutputStream
- 子类继承父类,可以创建对象
- 是一个字节一个字节的写出
创建对象
1 2
| OutputStream fos = new FileOutputStream(<str>); OutputStream fos = new FileOutputStream(<file>);
|
1
| OutputStream fos = new FileOutputStream(<str>, true);
|
写出
<char>
:需要写入的字符
BufferedOutputStream
- 子类继承父类,可以创建对象
- 是一个数组一个数组的写出
- 缓冲数组长度默认为8192
创建对象
1 2
| OutputStream fos = new FileOutputStream(<str>); OutputStream bos = new BufferedOutputStream(fos);
|
1 2
| OutputStream fos = new FileOutputStream(<str>); OutputStream bos = new BufferedOutputStream(fos, <len>);
|
1 2
| OutputStream fos = new FileOutputStream(<str>); OutputStream bos = new BufferedOutputStream(fos, <len>, true);
|
写出
字符流
字符读取流(输入流)
Reader
- 父类为抽象类,不能创建对象
- 字符流只能处理文本数据的文件
FileReader
- 子类继承父类,可以创建对象
- 是一个字节一个字节的读取
创建对象
1
| Reader fr = new FileReader(<str>);
|
1
| Reader fr = new FileReader(<file>);
|
读取
BufferedReader
- 子类继承父类,可以创建对象
- 是一个数组一个数组的读取
- 缓冲数组长度默认为8192
1 2
| Reader fr = new FileReader(<str>); Reader br = new BufferedReader(fr);
|
1 2
| Reader fr = new FileReader(<str>); Reader br = new BufferedReader(fr);
|
读取
创建对象
字符写出流(输出流)
Writer
- 父类为抽象类,不能创建对象
- 字符流只能处理文本数据的文件
FileWriter
- 子类继承父类,可以创建对象
- 是一个字节一个字节的写出
创建对象
1
| Writer fw = new FileWriter(<str>);
|
1
| Writer fw = new FileWriter(<file>);
|
1
| Writer fw = new FileWriter(<str>, true);
|
写出
<char>
:可以写出任何类型数据,例如字符串
BufferedWriter
- 子类继承父类,可以创建对象
- 是一个数组一个数组的写出
- 缓冲数组长度默认为8192
创建对象
1 2
| Writer fw = new FileWriter(<str>); Writer bw = new BufferedWriter(fw);
|
1 2
| Writer fw = new FileWriter(<str>, true); Writer bw = new BufferedWriter(fw);
|
写出
释放资源
<io>
:输入或输出流对象
1 2 3 4 5 6 7 8 9 10 11
| try { ... } catch (IOException e) { ... } finally { try { <io>.close(); } catch (IOException e) { ... } }
|
释放资源工具类
1 2 3 4 5 6 7
| public static void close(Closeable c) { try { ... } catch (IOException e) { ... } }
|
try with resource
- jdk1.7对于IO自动资源管理的优化
- 将创建流的代码作为try的参数传入,实现自动释放资源
1 2 3 4 5 6 7 8 9
| try ( InputStream in = new BufferedInputStream( new FileInputStream(from) ); OutputStream out = new BufferedOutputStream( new FileOutputStream(to) ); ) { ... } catch (IOException e) { ... }
|
文件复制
单字节读写
1 2 3 4 5 6 7 8
| private static void copy(File from, File to) { FileInputStream in = new FileInputStream(from); FileOutputStream out = new FileOutputStream(to); int b; while((b = in.read()) != -1) { out.write(b); } }
|
批量数据读写
1 2 3 4 5 6 7 8 9
| private static void copy(File from, File to) { FileInputStream in = new FileInputStream(from); FileOutputStream out = new FileOutputStream(to); byte[] buff = new byte[8192]; int n; while((n = in.read(buff)) != -1) { write(buff, 0, n); } }
|
序列化输出对象
反序列化恢复对象
Serializable接口
不序列化成员
静态属于类,不随对象一起被序列化输出
只在程序运行期间,在内从中临时存在,不随对象一起被序列化持久保存
序列化版本
1
| static final long serialVersionUID
|
根据类的定义信息,计算产生一个id值
字符集、字符编码
ASC-II
iso-8859-1(Latin-1)
cjk字符集
GBK
Unicode编码
- 统一码(万国码)
- 有100万以上编码位
- 常用字符表(双字节)
- 生僻字符表(三字节或以上)
Unicode中文字符
- 范围:
\u4e00
~\u9fa5
- 个数:20902个
- Unicode的传输格式
- 英文(单字节)
- 某些字符(双字节)
- 中文(三字节)
- 特殊符号(四字节)
Java的字符编码转换
Unicode转其他编码
其他编码转Unicode
1
| new String(byte[], "UTF-8")
|
Reader/Writer
方法
一个int参数
- int四个字节中,末尾两个字节是char类型字符数据,只处理末尾两个字节的输出
三个参数
1
| write(char[], from, length)
|
一个String参数
OutputStreamWriter
<path>
:文件路径
1 2
| new OutputStreamWriter(new FileOutputStream("<path>") new OutputStreamWriter(new FileOutputStream("<path>"), "UTF-8")
|
参考文献
达内教育