- package com.test20;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- //文本复制
- public class fileReader {
- public static void main(String[] args) throws Exception {
- long start = System.currentTimeMillis();
- method1("c:\\test\\heima.doc", "D:\\test\\c.doc");
- long end = System.currentTimeMillis();
- long time = end - start;
- System.out.println(time);
- }
- // 字符缓冲流一次读取一个 字符串 15毫秒
- // 如何实现数据的追加写入?
- // 用构造方法带第二个参数是true的情况即可
- private static void method1(String s1, String s2) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(s1));
- // BufferedReader br = new BufferedReader(new StringReader(s1));
- PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(s2, true)));
- int linecount = 0;
- String s;
- while ((s = br.readLine()) != null) {
- pw.println(linecount++ + ": " + s);
- }
- pw.close();
- }
- // 字符缓冲流一次读取一个 字符串 15毫秒
- private static void method2(String s1, String s2) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(s1));
- BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
- // PrintWriter pw = new PrintWriter((new FileWriter(s2,true)));
- int linecount = 0;
- String s;
- while ((s = br.readLine()) != null) {
- // pw.println(linecount++ +": "+s);//一句顶三句
- bw.write(linecount++ + ": " + s);
- bw.newLine();
- bw.flush();
- }
- bw.close();
- br.close();
- // pw.close();
- }
- // 字符缓冲流一次读取一个 字符数组 16毫秒
- private static void method3(String s1, String s2) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(s1));
- BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
- char[] char1 = new char[1024];
- int s = 0;
- while ((s = br.read(char1)) != -1) {
- bw.write(char1, 0, s);
- }
- br.close();
- bw.close();
- }
- // 字符缓冲流一次读取一个 字符 15毫秒
- private static void method4(String s1, String s2) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(s1));
- BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
- int s = 0;
- while ((s = br.read()) != -1) {
- bw.write(s);
- }
- br.close();
- bw.close();
- }
- // 基本字符流流一次读取一个 字符数组 15毫秒
- private static void method5(String s1, String s2) throws IOException {
- FileReader fr = new FileReader(s1);
- FileWriter fw = new FileWriter(s2);
- char[] char1 = new char[1024];
- int s = 0;
- while ((s = fr.read(char1)) != -1) {
- fw.write(char1, 0, s);
- }
- fr.close();
- fw.close();
- }
- // 基本字符流流一次读取一个 字符 31毫秒
- private static void method6(String s1, String s2) throws IOException {
- FileReader fr = new FileReader(s1);
- FileWriter fw = new FileWriter(s2);
- int s = 0;
- while ((s = fr.read()) != -1) {
- fw.write(s);
- }
- fr.close();
- fw.close();
- }
- }
复制代码 |