- package com.kxg.zifu;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class ZiFuDemo4 {
- public static void main(String[] args) throws IOException {
- String srcString = "a.txt";
- String destString1 = "1.txt";
- String destString2 = "2.txt";
- String destString3 = "3.txt";
- String destString4 = "4.txt";
- String destString5 = "5.txt";
- method1(srcString, destString1);
- method2(srcString, destString2);
- method3(srcString, destString3);
- method4(srcString, destString4);
- method5(srcString, destString5);
- }
- private static void method5(String srcString, String destString)
- throws IOException {
- BufferedReader br = new BufferedReader(new FileReader(srcString));
- BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
- String line = null;
- while ((line = br.readLine()) != null) {
- bw.write(line);
- bw.newLine();
- bw.flush();
- }
- br.close();
- bw.close();
- }
- private static void method4(String srcString, String destString)
- throws IOException {
- // 高效流一次读取一个字符数组
- BufferedReader br = new BufferedReader(new FileReader(srcString));
- BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
- char[] chs = new char[1024];
- int len = 0;
- while ((len = br.read(chs)) != -1) {
- bw.write(chs);
- }
- br.close();
- bw.close();
- }
- private static void method3(String srcString, String destString)
- throws IOException {
- // 高效流一次读写一个字符
- BufferedReader br = new BufferedReader(new FileReader(srcString));
- BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
- int i = 0;
- while ((i = br.read()) != -1) {
- bw.write(i);
- }
- br.close();
- bw.close();
- }
- private static void method2(String srcString, String destString)
- throws IOException {
- // 一次读写一个字符数组
- FileReader fr = new FileReader(srcString);
- FileWriter fw = new FileWriter(destString);
- char[] chs = new char[1024];
- int len = 0;
- while ((len = fr.read(chs)) != -1) {
- fw.write(chs, 0, len);
- }
- fr.close();
- fw.close();
- }
- private static void method1(String srcString, String destString)
- throws IOException {
- // 一次读写一个字符
- FileReader fr = new FileReader(srcString);
- FileWriter fw = new FileWriter(destString);
- int i = 0;
- while ((i = fr.read()) != -1) {
- fw.write(i);
- }
- fr.close();
- fw.close();
- }
- }
复制代码
|
|