Java-流
1.流
- 概念:一组有序的数据序列(字节数组)
- 分类:
① 方向
1) 输入流:从外到内; XxxInputStream/XxxReader
2) 输出流:从内到外; XxxOutputStream/Xxxwriter
② 内容
1) 字节流:byte[]; XxxInputStream/XxxOutputStream
2) 字符流:char[]; XxxReader/XxxWriter
③ 功能
1) 节点流:直接面向数据源的流(基础流)永远提供基础数据;
2) 处理流:扩展了节点流在某些特定领域的操作;(不能独立写,是基于节点流写的,是对节点流的补充及扩展)
④ 是否依赖网络
1) 本地流:文件流
2) 网络流 - 文件流:
① 目录
a. File dir = new File(“D:\software”); //创建目录对象
b. boolean exists = dir.exists(); //目录是否存在
c. boolean isDir = dir.isDirectory(); //是否是目录对象
d. boolean isFile = dir.isFile(); //是否是文件对象
e. File[] whats = dir.listFiles([FileNameFilter/FileFilter/filter]); //
f. boolean success = file.mkdirs(); //创建目录
② 文件
1)File
a. File file = new File(“D:\software”); //创建文件对象
b. boolean exists = file.exists(); //文件是否存在
c. boolean isFile = file.isFile(); //是否是文件对象
d. boolean isDir = file.isDirectory(); //是否是目录对象
e. String parentDir = file.getParent(); //获取父目录的字符串
f. String parent = file.getParent(); //获取父目录对象
g. String name = file.getName(); //获取文件名称
h. long realSize = file.length(); // 获取文件的实际字节数
i. long freeSpace = file.getFreeSpace(); //获取文件剩余可存字节数
j. long lastModified = file.lastModified(); //获取文件最后一次修改的长整时间
k. boolean success = file.createNewFile(); //创建文件
③ 文件流
1) 字符文件流
a. 字符文件输入流
a) FileReader fr = new FileReader(String/File path)
b) Int len = fr.read(char[] sc)
//len 实际读取的字符数;-1表示文件结尾
c) Fr.close;
b.字符文件输出流
a) FileWriter fw = new FileWriter(String/File path, Boolean append);
//默认为覆盖写入模式,append为true时表示追加模式
b) fw.write(char[] cs [,int beginindex,int count];
fw.write(String str[,int beginindex,int count];
c)fw.close
c. 字符缓存读写流
a) 读
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
//实际读取行的内容,null表示文件结尾
br.close
//同步关闭关联流资源,并释放文件资源
b) 写
BufferedWriter bw = new BufferedWriter(new FileReader(file))
bw.newline; //添加新行
…
br.close;
2) 字节文件流
a. 输入
a) FileInpuStream fis = new FileInputStream(String/file src);
BufferedInputSteam bis = new BufferedInputStream(fis,[int size]);
//如果src不存在则报,Java.io.FileNotFoundException;
b) int len = fis.read(byte[] bs);
c) fis.close();
b. 输出
a) FileOutputStream fos = new FileOutputStream(String/file dest);
BufferedOutputStream bis = new BufferedOutputStream(fis,[int size]);
//如果dest不存在则报,Java.io.FileNotFoundException;
b) fos.write(byte[] bs,int beginIndex,int count);
c) fos.close;