使用情况:如果数据通过Window自带的记事本软件打开,我们还可以读懂里面的内容,就使用字符流;否则使用字节流。如果不知道该使用那种类型的流,就使用字节流(万能流)。
FileWriter类(重点)
主要用于将文本内容写入到文本文件。
FileReader类(重点)
主要用于从文本文件读取文本数据内容。
FileOutputStream类(重点)
java.io.FileOutputStream类主要用于将图像数据之类的原始字节流写入到输出流中。
FileInputStream类(重点)
主要用于从输入流中以字节流的方式读取图像数据等。
FileInputStream类(重点)
主要用于从输入流中以字节流的方式读取图像数据等。
BufferedOutputStream类(重点)
主要用于描述缓冲输出流,此时不用为写入的每个字节调用底层 系统。
BufferedInputStream类(重点)
主要用于描述缓冲输入流。
BufferedWriter类(重点)
主要用于写入单个字符、字符数组以及字符串到输出流中。
BufferedReader类(重点)
用于从输入流中读取单个字符、字符数组以及字符串。
public class BufferedCharCopyTest {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 1.创建BufferedReader类型的对象与d:/a.txt文件关联
br = new BufferedReader(new FileReader("d:/a.txt"));
// 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
bw = new BufferedWriter(new FileWriter("d:/b.txt"));
// 3.不断地从输入流中读取一行字符串并写入到输出流中
System.out.println("正在玩命地拷贝...");
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine(); // 当前系统中的行分隔符是:\r\n
}
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭流对象并释放有关的资源
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class BufferedByteCopyTest {
public static void main(String[] args) {
// 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
long g1 = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.创建BufferedInputStream类型的对象与d:/02_IO流的框架结构.mp4文件关联
bis = new BufferedInputStream(new FileInputStream("d:/02_IO流的框架结构.mp4"));
// 2.创建BufferedOuputStream类型的对象与d:/IO流的框架结构.mp4文件关联
bos = new BufferedOutputStream(new FileOutputStream("d:/IO流的框架结构.mp4"));
// 3.不断地从输入流中读取数据并写入到输出流中
System.out.println("正在玩命地拷贝...");
byte[] bArr = new byte[1024];
int res = 0;
while ((res = bis.read(bArr)) != -1) {
bos.write(bArr, 0, res);
}
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭流对象并释放有关的资源
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long g2 = System.currentTimeMillis();
System.out.println("使用缓冲区拷贝视频文件消耗的时间为:" + (g2-g1)); // 44
}
}