一 体系结构
1 按流向分
- 输出流
- 输入流
2 按数据类型分
1. 字节流
输入流 InputStream
- 读取方法
- int read() 一次读取一个字节
- int read(byte [] bys) 一次读取一个字节数组
- 实现类
- FileInputStream
- BufferedInputStream 高效字节输入流
输出流 OutputStream
- 读取方法
- void write (int by) 一次写一个字节
- void write (byte [] bys, int 1,int len) 一次写一个字节数组
- 实现类
- FileOutputStream
- BufferedOutputStream 高效字节输出流
转换流
- 转换输出流 OutputStreamWriter(InputStream is 需要的是其子类对象 FileInputStream)
- 转换输入流 InputStreamReader(Outputstream os 需要的是其子类对象FileOutputStream)
2. 字符流=字节流 +编码表(默认为简体中文编码表GBK)
输入流 Reader
- 读取方法
- int read() 一次读取一个字符
- int read(char [] chs) 一次读取一个字符数组
- readline 一次读 一行
- 实现类
- OutputStreamReader
- FileReader 简单子类
- BufferedReader 高效字符输入流
输出流 Write
- 写入方法
- void write (int by) 一次写一个字符
- void write (char [] chs, int 1,int len) 一次写一个字符数组
- 实现类
- InputStreamWrite
- FileWrite 简单子类
- BufferedWrite 高效字符输出流
二总结
封装目录
Field fiel =new File (“a.txt”);
字节流构造方法
FileOutputStream fos=new FileOutputStream (file);
FileInputStream fis=new FileInputStream(file);
高效字节流
BufferedOutputStrream bos =new BufferedOutputstream (new FileOutputStream(new File(“a.txt”)));
BufferedInputStream bis =new BufferedInputStream (new FileInputStream (new File(“a.txt”)))
字符流构造方法
OutputStreamReader osr=new OutputStreamReader ( new FileOutputStream( new File (“a.txt”) ) );
IputStreamWrite isw=new InputStreamWrite (new FileInputStream ( new File (“a.txt”) ) );
字符流简版构造方法
FielReader fr=new FileReader (new File(“a.txt”) );
FileWrite fw=new FileWrite ( new File(“a.txt”) );
高效字符流
BufferedReader br = new BufferedReader (new FileReader(“a.txt”));
BufferedWrite bw = new BufferedWrite (new FileWrite(“a.txt”));
三 特记
除了用Windows 记事本打开 能读懂的数据用字符流外 其他一般用 字节流
字节流复制4种方式 字符流 5种方式 一种 readline 为特有
四 常用:
常用构造方法
- 字节流
BufferedOutputStream bos= new BufferedOutputStream (new FileOutputStream(“a.txt”))
BufferedInputStream bis= new BufferedInputStream (new FileInputStream (“a.txt”));
字符流
BufferedReader br=new BufferedReader(new FileReader(“a.txt”));
BufferedWrite bw=new BufferedWrite (new FileWrite(“a.txt”));- 常用方法
- 字节流 BufferOutputStream BufferedInputStream
- 字符流 BufferedWrite 一次写一个字符串
BufferedReader 一次读一行 readline
五 常用方法
File :
- 一般方法:
- mkdirs 创建多级文件夹
- mkdir 创建文件夹
- CreateNewFile 创建文件
- Delete 删除 注意 只能一级 的删 不走回收站
- IsDirectory 是否是文件夹
- IsFile 是否是文件
- IsHidden 是否隐藏
- CanRead 是可读
- CanWrite 是否可写
- exits 是否存在
- renameto 重命名 代码体现如下
File file1=new File("a.txt");
File file2=new File ("b.txt");
file1.renameto(file2);
基本获取功能:
- getAbsolutePath 获取绝对路径
- getPath 获取相对路径
- getName 获取名称
- getlengh 获取长度(单位字节)
- astmodified 最后一次修改时间(单位毫秒值);
高级获取功能:
- List 获取指定目录下所有文件或文件夹的名称数组
File fiel=new File("e:\\");
String [ ] name=file.list();
- ###### FileList 获取指定目录下所有文件或文件夹的 File数组
File file=new File ("e:\\");
File [ ] filearray=file.listfile();
例子
- 文件过滤器:
重写一个接口的accept方法 获取指定目录下的File数组
File file=new File ("e:\\");
File [] fileArray = file.listfiles(new FileNamefilter(){
@cvervide
public boolean accpet(File dir,String name){
return new File(dir,name).IsFile()&&name.endWith(".java");
}
});
六 其他各种流
1 数据操作流
(1) 可以操作基本类型的数据
(2) 流对象名称
DataInputStream DataOutputStream
2 内存操作流(在内存中 一数组的形式操作)
(1) 有些时候我们操作完毕之后 未必需要产生一个文件
就可以使用内存操作流 在内存中操作文件
(2) 流对象名称
- ByteArrayInputStream
- ByteArrayOutputStream
- CharArrayReader
- CharArrayWrite
- StringReader
- StringWrite
3 打印流
(1) 分类
- 字节打印流
- 字符打印流
(2) 特点:
- 只操作目的地 ,不操作数据源
- 可以操作任意类型的数据
- 如果启用了自动刷新 在调用Println()方法的时候,能够换行并刷新
- 可以直接操作文件哪些流可以直接操作文件呢?
- 构造方法能够同时接受File 和String 类型的参数 就可以直接操作文件
(3) 复制文本文件
BufferedReader br=new BufferedReader(new FileReader("a.txt"));
PrintWrite pw=new PrintWrite(new FileWrite("b.txt"));
String line=null;
While((line=br.readLine())!=null){
pw.println(line);
}
pw.close();
br.close();
4 标准输入输出流
(1) System类下的
- in 标准输入流
- out 标准输出流
(2) 三种键盘录入方式
- main 方法的的args[] 接受参数
- System.in 通过BufferedReader 进行包装
BufferedReader br=new BufferedReader
(new InputStreamReader(System.in) );
- Scanner sc=new Scanner(System.in);
(3) 输出语句的原理和如何使用字符流 输出数据
- 原理
System.out.println("helloword");
PrintStream ps=System.out;
ps.println();
- 把System.out 用字符缓冲流包装使用
BufferedWrite bw=new BufferedWrite(new OutputStreamWrite(System.out));
5 随机访问流
(1) 可以按照文件指针的位置写数据和读数据
(2) 步骤
- 写数据
- 读数据
- 获取和改变文件指针位置
6 合并流
(1) 把多个输出流数据写到一个输出流里面
(2) 构造方法
- SequerceInputStream(InputStream s1, InputStream s2)
- SequenceInputStream(Enumeration
7 序列化流
(1) 可以把对象写入文本文件或者在网路中传输
(2) 如何实现序列化
- 让被序列化的对象所属的类 实现序列化接口该接口是一个标记没用功能需要实现
(3) 注意问题
把数据写入一个文件后 去修改类会产生一个问题
解决问题的方法: 在类文件中 给出一个固定的序列化id值
(4)对象流
ObjectOutputStream ObjectInputStream
1. 构造方法
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("obj.txt"));
ObjectInputStream ois=new ObjectInputStream (new FileInputStream("obj.txt"));
- 普通方法
- writeObject();
- ReadObjec():
(5) 面试题
- 什么时候序列化 : 把对象写入文件或者在网络中传输
- 如何实现序列化 : 让对象所属的类实现序列化接口
- 什么是反序列化 : 把文本文件的流还原成对象
8 Properties
(1) 是一个集合类 是Hashtable 的子类
(2)特有功能
- public Object setProperty(String key,String value) 获得键值对类型
- public String getProperty(String key) 根据键获得值
- public Set stringPropertyNames() 获得所有键的集合
(3) 和IO流结合使用的方法
- 把键值对形式的文本文件加载到集合中
- public void load(Reader reader)
- public void load(InputStream inStream)
- 把集合中的数据存储到文本文件中
- public void store (Write write,String comments)
- public void Store(OutputStream out,String comments)
(4) 案例
根据给定的文件判断是否有键 为 “lisi”的 如果有就改值为100
思路体现:
- 把文本文件的数据加载(load)到Properties 集合中
- 通过获取Properties集合中所有的键
- 遍历所有键的集合 判断是否有”lisi” 有就 改100
- 然后把修改后的集合 存储(store)到文件中
写一个程序 控制猜数字小游戏 只能玩5次
思路体现:
- 创造一个文本文件”count.txt”内容为 count=0;
- 每玩一次 就把该文件加载(load)到Properties集合中 然后同count键得到对应的值 若<5 就可以玩 然后修改 count的值 count++
- 最后把修改后的集合 存储(store)到”count.txt”文件中
- 回到2
9 NIO(new IO)
(1) jdk4出现的nio 对以前的IO操作进行优化 提供了效率
(2) jdk7 的nio 及使用
- Path
- Paths
- Files: 提供常见的功能
- 复制文本文件
- 把集合中的数据写到文本文件
10 最基本的读取代码体现
- 递归删除文件夹下 所有内容
- 使用递归写一个方法 能删除一个文件夹下的所有文件
//先删除目录下的内容 再删除目录
public static void deleteAllFiles(File file){
System.out.println("文件夹————"+file.getName());
File[] fs = file.listFiles();
for (File f : fs) {
if (f.isDirectory()) {
//继续遍历
deleteAllFiles(f);
}else{
System.out.println("删除文件:"+f.getName());
f.delete();
}
}
//删除目录
file.delete();
}
public static void main(String[] args) {
File file = new File("E:\\a");
deleteAllFiles(file);
}
}
- 文件复制
/*
* 将aaa.txt拷贝到bbb.txt
*
* 1.读aaa.txt
* 2.写入bbb.txt
*
*/
public class CopyDemo {
public static void main(String[] args){
//File f1 = new File("E:\\aaa.txt");
File f2 = new File("E:\\bbb.txt");
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("E:\\aaa.txt");
fw = new FileWriter(f2);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
System.out.println("copy ok!");
} catch (FileNotFoundException e) {
System.err.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流资源 后开的先关闭
if (fw != null) {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}