第一个案例!
负责文本文件的5种方式(字符流)
- package cn.itcast_01;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class CopyFileDemo {
- public static void main(String[] args) throws IOException {
- File srcFile = new File("D:\\copy01\\a.txt");
- File destFile = new File("D:\\copy02\\a.txt");
- // mothed1(srcFile, destFile);
- //mothed2(srcFile, destFile);
- //mothed3(srcFile, destFile);
- //mothed4(srcFile, destFile);
- mothed5(srcFile, destFile);
- }
- public static void mothed1(File srcFile, File destFile) throws IOException {
- FileWriter fw = new FileWriter(destFile);
- FileReader fr = new FileReader(srcFile);
- for (int by = 0; (by = fr.read()) != -1;) {
- fw.write(by);
- }
- fr.close();
- fw.close();
- }
- public static void mothed2(File srcFile, File destFile) throws IOException {
- BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
- BufferedReader br = new BufferedReader(new FileReader(srcFile));
- for (int by = 0; (by = br.read()) != -1;) {
- bw.write(by);
- }
- br.close();
- bw.close();
- }
- public static void mothed3(File srcFile, File destFile) throws IOException {
- FileWriter fw = new FileWriter(destFile);
- FileReader fr = new FileReader(srcFile);
- char[] chs = new char[1024];
- int len = 0;
- while ((len = fr.read(chs)) != -1) {
- fw.write(chs, 0, len);
- }
- fr.close();
- fw.close();
- }
-
- public static void mothed4(File srcFile, File destFile) throws IOException {
- BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
- BufferedReader br = new BufferedReader(new FileReader(srcFile));
- char[] chs = new char[1024];
- int len = 0;
- while ((len = br.read(chs)) != -1) {
- bw.write(chs, 0, len);
- }
- br.close();
- bw.close();
- }
-
- public static void mothed5(File srcFile, File destFile) throws IOException {
- BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
- BufferedReader br = new BufferedReader(new FileReader(srcFile));
- String line=null;
- while ((line = br.readLine()) != null) {
- bw.write(line);
- bw.newLine();
- bw.flush();
- }
- br.close();
- bw.close();
- }
- }
复制代码 第二个案例!
复制图片的4种方法!(字节流)
- package cn.itcast_02;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class CopyJpgDemo {
- public static void main(String[] args) throws IOException {
- File srcFile = new File("D:\\copy01\\a.jpg");
- File destFile = new File("D:\\copy02\\a.jpg");
- // mothed1(srcFile, destFile);
- //mothed2(srcFile, destFile);
- //mothed3(srcFile, destFile);
- mothed4(srcFile, destFile);
- }
- public static void mothed1(File srcFile, File destFile) throws IOException {
- FileOutputStream fos = new FileOutputStream(destFile);
- FileInputStream fis = new FileInputStream(srcFile);
- for (int by = 0; (by = fis.read()) != -1;) {
- fos.write(by);
- }
- fos.close();
- fis.close();
- }
- public static void mothed2(File srcFile, File destFile) throws IOException {
- FileOutputStream fos = new FileOutputStream(destFile);
- FileInputStream fis = new FileInputStream(srcFile);
- byte[] bys = new byte[1024];
- int len = 0;
- while ((len = fis.read(bys)) != -1) {
- fos.write(bys, 0, len);
- }
- fos.close();
- fis.close();
- }
- public static void mothed3(File srcFile, File destFile) throws IOException {
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
- for (int by = 0; (by = bis.read()) != -1;) {
- bos.write(by);
- }
- bos.close();
- bis.close();
- }
- public static void mothed4(File srcFile, File destFile) throws IOException {
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
- byte[] bys = new byte[1024];
- int len = 0;
- while ((len = bis.read(bys)) != -1) {
- bos.write(bys, 0, len);
- }
- bos.close();
- bis.close();
- }
- }
复制代码
|
|