JAVA文件读写示例

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.IOException;

public class StreamTest {  
    public static void main(String[] args) {
//        readFileDemo2("c:/1.txt");
        writeFileDemo2("c:/1.pdf","c:/2.pdf");
    }
    private static void readFileDemo1(String path){
        FileReader fr = null;
        BufferedReader br  = null;
        try {
            File file = new File(path);
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            String line = "";
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(br != null) br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void readFileDemo2(String path){
        FileInputStream fis = null;
        BufferedInputStream bis  = null;
        try {
            File file = new File(path);
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            int len = 0;
            int buf = 0;
            while((buf=bis.read()) != -1){
                System.out.print((char)buf);
                len++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(bis != null) bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void writeFileDemo1(String path1,String path2){
        File file1 = new File(path1);
        File file2 = new File(path2);
        FileReader fr = null;
        BufferedReader br = null;
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fr = new FileReader(file1);
            br = new BufferedReader(fr);
            fw = new FileWriter(file2);
            bw = new BufferedWriter(fw);
            String line = "";
            while((line=br.readLine()) != null){
                bw.write(line+"\n");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(br != null) br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bw != null) bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static void writeFileDemo2(String path1,String path2){
        File file1 = new File(path1);
        File file2 = new File(path2);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file1);
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(file2);
            bos = new BufferedOutputStream(fos);
            int tmp = 0;
            while((tmp = bis.read()) != -1){
                bos.write(tmp);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(bis != null) bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bos != null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

zhutougg

继续阅读此作者的更多文章