本帖最后由 marcojam 于 2015-8-8 11:56 编辑
第一次发帖,之前都没做笔记。
字符流是用于文本操作的类,字符流= 节流+编码表,相当于是通过转换流将字节流转换成字符流
- package 字符流复制文本5种方式;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class FiveCopyStream {
- public static void main(String[] args) throws IOException {
- // TODO 自动生成的方法存根
- // 方式一:基本字符复制
- FileReader fr = new FileReader("e:\\ArrayListDemo.java");
- FileWriter fw = new FileWriter("f:\\ArrayListDemo.java");
- int b = 0;
- while ((b = fr.read()) != -1) {
- fw.write(b);
- }
- fw.close();
- fr.close();
- // 方式二:基本字符数组流
- FileReader fr1 = new FileReader("e:\\ArrayListDemo.java");
- FileWriter fw1 = new FileWriter("f:\\ArrayListDemo1.java");
- int c = 0;
- char[] ch = new char[1024];
- while ((c = fr1.read(ch)) != -1) {
- fw1.write(ch, 0, c);
- }
- fw1.close();
- fr1.close();
- // 方式三:高效缓冲基本字符流
- BufferedReader br = new BufferedReader(new FileReader(
- "e:\\ArrayListDemo.java"));
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- "f:\\ArrayListDemo2.java"));
- int d = 0;
- while ((d = br.read()) != -1) {
- bw.write(d);
- }
- bw.close();
- br.close();
- //方式四:高效缓冲字符数组流
- BufferedReader br1 = new BufferedReader(new FileReader(
- "e:\\ArrayListDemo.java"));
- BufferedWriter bw1 = new BufferedWriter(new FileWriter(
- "f:\\ArrayListDemo3.java"));
- int e = 0;
- char[] ch1 = new char[1024];
- while((e = br1.read(ch1))!=-1){
- bw1.write(ch1, 0, e);
- }
- bw1.close();
- br1.close();
- //方式五:高效缓冲行读取
- BufferedReader br2 = new BufferedReader(new FileReader(
- "e:\\ArrayListDemo.java"));
- BufferedWriter bw2 = new BufferedWriter(new FileWriter(
- "f:\\ArrayListDemo4.java"));
- String s = null;
- while((s = br2.readLine())!=null){
- bw2.write(s);
- bw2.newLine();
- bw2.flush();
- }
- bw2.close();
- br2.close();
- }
- }
复制代码
|
|