字符流
- 特点:把字符串转换成字节数组,只能读写文本文件
Reader | Writer |
---|---|
字符输入流 读 | 字符输出流 写 |
编码 | 解码 |
---|---|
public byte[] getBytes() | public String(byte[] bytes) |
使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 | 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
public byte[] getBytes(String charsetName) | public String(byte[] bytes, String charsetName) |
通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。 | 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。 |
4.方法概述
方法 | 作用 |
---|---|
public void write(int c) | 写一个字符 |
public void write(char[] cbuf) | 写一个字符数组 |
public void write(char[] cbuf,int off,int len) | 写一个字符数组的 一部分 |
public void write(String str) | 写一个字符串 |
public void write(String str,int off,int len) | 写一个字符串的一部分 |
5.高效字符流
BufferedWriter | BufferedReader |
---|---|
public void newLine() | public String readLine() |
根据系统来决定换行符 | 一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null |
请编写程序,采用字符流多种方式,完成文本文件的复制,并测试
基本的流一次一个字符
基本的流一次一个字符数组
高效的流一次一个字符
高效的流一次一个字符数组
高效的流一次一行字符串
public class demo4{
public static void main(String[]
args) throws IOException {
String
srcPath = "a.txt";
String
destPath = "b.txt";
//method1(srcPath,destPath);
//method2(srcPath,destPath);
//method3(srcPath,destPath);
//method4(srcPath,destPath);
method5(srcPath,destPath);
}
public
static void method1(String srcPath, String destPath) throws IOException {
FileReader
fr = new FileReader(srcPath);
FileWriter fw = new
FileWriter(destPath);
int
ch = 0;
while
((ch = fr.read()) != -1) {
fw.write(ch);
}
fr.close();
fw.close();
}
public
static void method2(String srcPath, String destPath) throws IOException {
FileReader
fr = new FileReader(srcPath);
FileWriter
fw = new FileWriter(destPath);
//读
char[]
buffer = new char[1024];
int
len = 0;
while((len
= fr.read(buffer)) != -1){
//写
fw.write(buffer,
0, len);
}
fr.close();
fw.close();
}
public
static void method3(String srcPath, String destPath) throws IOException {
BufferedReader
br = new BufferedReader(new FileReader(srcPath));
BufferedWriter
bw = new BufferedWriter(new FileWriter(destPath));
//读
int
ch = 0;
while
((ch = br.read()) != -1) {
//写
bw.write(ch);
}
br.close();
bw.close();
}
public static void method4(String srcPath,
String destPath) throws IOException {
BufferedReader
br = new BufferedReader(new FileReader(srcPath));
BufferedWriter
bw = new BufferedWriter(new FileWriter(destPath));
//读
char[]
buffer = new char[1024];
int
len = 0;
while((len
= br.read(buffer)) != -1){
//写
bw.write(buffer,
0, len);
}
br.close();
bw.close();
}
public
static void method5(String srcPath, String destPath) throws IOException {
BufferedReader
br = new BufferedReader(new FileReader(srcPath));
BufferedWriter
bw =new BufferedWriter(new FileWriter(destPath));
//
String
line = null;
while
((line = br.readLine()) != null) {
//
bw.write(line);
}
br.close();
bw.close();
}
}
字节流
1.特点:可以读写任何类型的文件 比如音频 视频 文本文件
2.
InputStream | OutputStream |
---|---|
字节输入流 | 字节输出流 |
3.write()方法
方法 | 作用 |
---|---|
public void write(int b) | 写一个字节 超过一个字节 砍掉前面的字节 |
public void write(byte[] b) | 写一个字节数组 |
public void write(byte[] b,int off,int len) | 写一个字节数组的一部分 |
编写程序,采用多种方式,把d:\video01.avi的内容复制到d:\video02.avi中
方式1:基本字节流一次读写一个字节
方式2:基本字节流一次读写一个字节数组
方式3:高效字节流一次读写一个字节
方式4:高效字节流一次读写一个字节数组
public class demo3 {
public
static void main(String[] args) throws IOException {
//method1("D:\\video01.avi",
"D:\\video02.avi");
//method2("D:\\video01.avi",
"D:\\video02.avi");
//method3("D:\\video01.avi",
"D:\\video02.avi");
method4("D:\\video01.avi",
"D:\\video02.avi");
}
public static void method1(String srcPath,
String destPath) throws IOException {
FileInputStream
fis = new FileInputStream(srcPath);
FileOutputStream
fos = new FileOutputStream(destPath);
int
by = 0;
fos.write(by);
}
fis.close();
fos.close();
}
public
static void method2(String srcPath, String destPath) throws IOException {
FileInputStream
fis = new FileInputStream(srcPath);
FileOutputStream
fos = new FileOutputStream(destPath);
byte[]
buffer = new byte[1024];
int
len = 0;
while(
(len=fis.read(buffer)) != -1){
fos.write(buffer,
0, len);
}
fos.close();
fis.close();
}
public
static void method3(String srcPath, String destPath) throws IOException {
BufferedInputStream
bis = new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStream
bos = new BufferedOutputStream(new FileOutputStream(destPath));
int
by = 0;
while
(( by=bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
public
static void method4(String srcPath, String destPath) throws IOException {
BufferedInputStream
bis = new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStream
bos = new BufferedOutputStream(new FileOutputStream(destPath));
byte[]
buffer = new byte[1024];
int
len = 0;
while
((len = bis.read(buffer)) != -1) {
bos.write(buffer,
0, len);
}
bos.close();
bis.close();
}
}