什么是流?
(1) 概念:一组有序的数据序列(字节数组)
(2) 分类
① 方向
1)输入:从外→内 XxxInputStream / XxxReader
2)输出:从内→外 XxxOutputStream / XxxWriter
② 内容
1) 字节流:byte[] XxxInputStream / XxxOutputStream
2) 字符流:char[] XxxReader / XxxWriter
③ 功能
1) 节点流:直接面向数据源的流(基础流)
2) 处理流:扩展了节点流再某些特定领域的操作
④ 是否依赖网络
1)本地流:文件流
2)网络
(3)文件流
① 目录
1)File
a File dir = new File(String path); //创建目录
b boolean exists = dir.exists(); //目录是否存在
c boolean isDir = dir.isDirectory //是否是目录
d File[] wahts = dir.listFiles([FileNameFilter/FileFilter filter]);
e boolean success = dir.mkdir(s);
f 关于相对路径和绝对路径
a)普通Java工程相对路径为项目的根路径
b)绝对路径:从分区根目录开始的路径
② 文件
a File file = new File(String path); //创建对象
b boolean exists = file.exists(); //文件是否存在
c boolean isFile = file.isFile(); //是否是文件
d String parentSir = file.getParent(); //获取父目录字符串
e File parent = file.getParentFile(); //获取父目录对象
f String fileName = file.getNmae(); //获取文件名
g long realSize = file.length(); //获取文件实际使用字节数
h long freeSpace = file.getFreeSpace; //获取文件剩余可存字节数
i long lastMod = file.lastModified(); //获取文件上一次修改时间的长整数
j boolean success = file,createNewFile(); //创建文件
③ 文件流
1) 字符文件流
a. 字符文件输入流
a) FileReader fr = new FileReader(String/File path);
b) int len = fr.read(char[] cs); //len:实际读取的字符数,-1表示文件结尾。
b. 字符文件输出流
a)FileWriter fw = new FileWriter(String/file path,boolean append);
默认为覆盖写入模式,append:true表示追加模式
b) fw.writer(char[] cs[,int beginindex,int count]);
fw.writer(String str,int beginindex,int count);
c). fw.close(); 释放文件资源
c. 字符缓存读写流
a) 读
i. BufferedReader br = new BufferedReader(FileReader fr);
ii. String line = br.readLine(); //line:实际读取行的内容,null表示文件结尾
iii. br.close();//同步关闭关联流资源,并释放文件资源
b) 写:
i. BufferedWriter bw = new BufferedWriter(FileWriter fw);
ii. bw.newLine(); //添加行
iii. …
iiii. bw.close();
2) 字节文件流
a.输入
a) FileInputStream fis = new FileInputStream(String/File src);
BufferedInputStream fis = new BufferedInputStream(String/File src);
如果src不存在则报:java.io.FileNotFoundException
b) int len = fis.read(byte[] bs);
c) fis.close();
b.输出
a) FileOutputStream fos = new FileOutputStream(String/File dest);
BufferedOutputStream fis = new BufferedOutputStream(String/File src);
如果dest的目录不存在则报:java.io.FileNotFoundException
b) fos.write(byte[] bs,int beginIndex,int count);
c) fos.close();