1. 字节流
1. 输入流InputStream
1. int read():一次读取一个字节
2. int read(byte[] bys):一次读取一个字节数组
3. FileInputStream
4. BufferedInputStream
2. 输出流OutputStream
1. void write(int by):一次写一个字节
2. void write(byte[] bys,int i, int len):一次写一个字节数组的一部分
3. FileOutputStream
4. BufferedOutputStream
2. 字符流 = 字节流 + 编码表
1. 输入流Reader
1. int read():一次读取一个字符
2. int read(char[] chs):一次读取一个字符数组
3. InputStreamReader
1. FileReader
4. BufferedReader
1. String readLine():一次读取一行数据
2. 输出流Writer
1. void write(int ch):一次写一个字符
2. void write(char[] chs,int i,int len):一次写一个字符数组的一部分
3. OutputStreamWriter
4. FileWriter
4. BufferedWriter
1. void newLine():写换行符号
2. void write(String s):写一个字符串
除了用windows记事本打开能读懂的数据用字符流以外,其他的全部使用字节流。
- 字节流复制数据:4种方式
- 字符流复制数据:5种方式
- ● 输入输出
- ○ 入还是出,是相对于内存来说的!
- ○ 把数据读到内存中,称为输入,即input,进行数据read操作。
- ○ 从内存往外部设备写数据,成为输出 ,output,进行数据的write操作。
复制文件
● 复制文本文档(9种方法):
- ○ 字节流
- ■ 两种普通
- ● 一次读一个字节
- ● 一次读一个字节数组
- ■ 两种高效
- ● 一次读一个字节
- ● 一次读一个字节数组
- ■ 两种普通
- ○ 字符流
- ■ 两种普通
- ● 一次读一个字符
- ● 一次读一个字符数组
- ■ 两种高效
- ● 一次读一个字符
- ● 一次读一个字符数组
- ■ 两种普通
- ○ 字节流
● 复制视频或者照片(4种方法、不能用字符流)
- ○ 字节流
- ■ 两种普通
- ● 一次读一个字节
- ● 一次读一个字节数组
- ■ 两种高效
- ● 一次读一个字节
- ● 一次读一个字节数组
- ■ 一种特殊用法
- ■ 两种普通
- ○ 字节流
复制文本文件
- 字节流:
/**
*
* @author 朝九晚十
*
* 需求: 把 e:\\Test\\三国.avi 复制到 h:\\Test\\三国.avi中
*
* 字节流四种方法赋值文件:
* 1、基本字节流一次读写一个字节
* 2、基本字节流一次读写一个字节数组
* 3、高效字节流一次读写一个字节
* 4、高效字节流一次读写一个字节流
*
*/
public class CopyAviDemo {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//method1("e:\\Test\\三国.avi", "h:\\Test\\Copy1.avi");
// method2("e:\\Test\\三国.avi","h:\\Test\\Copy2.avi");
// method3("e:\\Test\\三国.avi","h:\\Test\\Copy3.avi");
method4("e:\\Test\\三国.avi","h:\\Test\\Copy4.avi");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
//1、基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
// 封装数据源(读)
FileInputStream fis = new FileInputStream(srcString);
// 封装目的地(写)
FileOutputStream fos = new FileOutputStream(destString);
//复制数据
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源(先关谁都行)
fos.close();
fis.close();
}
// 2、基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
//定义数组每次传一个数组
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);//从0到len的长度
}
// 释放资源
fos.close();
fis.close();
}
//3、高效字节流一次读写一个字节
public static void method3(String srcString, String destString)
throws IOException, Exception {
//创建一个缓存区
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while((by = bis.read())!=-1){
bos.wait(by);
}
// 释放资源
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节流
//4、高效字节流一次读写一个字节流
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// 释放资源
bos.close();
bis.close();
}
}
2.字符流(转换流):
/*
1.
2. @author 朝九晚十
3.
4. 字符流复制文件
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// method1("a.txt", "b.txt");
//method2("a.txt", "b.txt");
// method3("a.txt", "b.txt"); //(没写)
method4("a.txt", "b.txt");
}
// 一次一个字符
private static void method1(String str1, String str2) throws IOException {
// 封装数据源
FileReader fr = new FileReader(str1);
// 封装目的地
FileWriter fw = new FileWriter(str2);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
// 释放资源
fw.close();
fr.close();
}
//一次一个字符组
private static void method2(String string, String string2) throws IOException {
// 封装数据源
FileReader fr = new FileReader(string);
// 封装目的地
FileWriter fw = new FileWriter(string2);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs,0,len);
fw.flush();
}
// 释放资源
fw.close();
fr.close();
}
/*
* 高效缓冲字符流
* 将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的
* 高效写入
* 可以指定缓冲区的大小,或者接受默认的大小。在多数情况下,默认值就足够
* 大了。
*/
//一次赋值一个字符数组
private static void method4(String string, String string2) throws IOException{
//默认值构造方法: BufferedWriter(Writer out)
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
//两种方式(只用一次复制一个字符数组的方式)
char[] chs = new char[1024];
int len = 0;
while((len = br.read(chs))!=-1){
bw.write(chs,0,len);
bw.flush();
}
// 释放资源
fw.close();
fr.close();
}
}
字符缓冲流的特殊用法:
/*
* @author 朝九晚十
*
* 字符缓冲流的特殊方法
* BufferedWriter:
* public void newLine();
* BufferedReader:
* public void readLine();
*
*/
/*
* 复制文件
*/
public class BufferedDemo2 {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("fos2.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("fos3.txt",true));//追加输入
//按行读取数据
String line = null;
//读到是换行符时结束读取、返回null
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine(); //换行
bw.flush();
}
//释放资源
bw.close();
br.close();
}
}