字符流
- 特點:把字符串轉換成字節數組,只能讀寫文本文件
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();
}
}