1. 复制文件
1.1 字节流复制任意文件
|-- 字节数组缓冲
|-- 缓冲区对象复制
2. 字节流缓冲区
2.1 BufferedOutputStream
|-- 提高字节输出流的效率
|-- 构造器,传递任意的字节输出流 FileOutputStream
|-- write 写单个字节,字节数组
2.2 BufferedInputStream
|-- 提高字节输入流的效率
|-- 构造器,传递任意的自己诶输入流 FileInputStream
|-- read 读取单个字节,字节数组
3. 转换流
3.1 InputStreamReader
|-- 字节流向字符
|-- 构造方法,传递任意字节输入流,写字符集名字
|-- 读取的方法 读取单个字符,字符数组
3.2 OutputStreamWriter
|-- 字符流向字节
|-- 构造方法,传递任意字节输出流,写字符集名字
|-- 写的方法 单个字符,字符数组,字符串
|-- flush
什么是转换流?举例说明什么时候使用转换流?
.InputStreamReader和OutputStreamWriter, 前者将字节输入流转换为字符输入流, 后者将字符输出流转换为字节输出流
public class FileImageDemo {
public static void main(String[] args) throws IOException {
// 复制文件啊
FileReader fr =new FileReader ("a.txt");
FileWriter fw = new FileWriter ("b.txt");
// 定义字符数组 ,提高效率
char [] cha =new char[1024];
int len = 0;
while((len =fr.read(cha))!=-1){
fw.write(cha,0,len);
} //部分数据,从零开始,到几
fr.close();
fw.close();
}
}
打印流是什么?flush()和close()方法有什么区别?
1.PrintStream和PrintWriter
2.flush()方法将字符流从内存刷新写入到目的数据, close()方法关闭流, 释放资源
/*
* date.java 复制到copy .java中
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源, ,打印流
BufferedReader br = new BufferedReader(new FileReader("date.java"));
// 封装目的地
PrintWriter pw = new PrintWriter(new FileWriter("copy.java"));
String line = null;
while ((line = br.readLine()) != null) {
pw.println(line);
}
pw.close();
br.close();
}
}
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
// 创建File 对象
File src = new File("c:\\Dc");
File dest = new File("d:\\dc");
//判断文件不存在 就创建
if (!dest.exists()) {
dest.mkdir();
}
File[] fileArr = src.listFiles();
// 增强强for
for (File file : fileArr) {
String name = file.getName();
File newFile = new File(dest, name); // dest拿的是c:\\dc name
// 拿的是c:\\src
copy(file, newFile);
}
}
public static void copy(File file, File newFile) {
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys,0,len);
}
} catch (IOException e) {
throw new RuntimeException("目录复制失败");
}
}
}
public static void main(String[] args)throws IOException, InterruptedException {
//创建字节输出流 传递字符串文件
FileOutputStream fos = new FileOutputStream ("F:\\1.txt");
// 调用方法write 写数据 写单个字节
fos.wait(97);
// 写字节数组
fos.write("\r\n".getBytes());
fos.write("\r\nNilaia".getBytes());
// 写入字节数组一部分
byte[] bytes ={102,104,103,105,106};
fos.write(bytes,0,3); // 写一部分数据 从哪个索引开始, 写几个
// 关闭资源
fos.close();
}
}
// 创建2个字节流缓冲区对象,不要new
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建字节输入流缓冲区对象,传递字节输入流对象
bis = new BufferedInputStream(new FileInputStream("f:\\a"));
// 创建字节输出流缓冲区对象,传递字节输入流对象
bos = new BufferedOutputStream(new FileOutputStream("f:\\a"));
// 定义字节数组
byte[] b = new byte[1023];
// 定义变量 while 循环 字节输入流 读取单个单词 \\ 字节输出流的方法write传参数
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (IOException ex) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException ex) {
throw new RuntimeException("关闭资源失败");
}
}
}
}
}