java实现文件copy

package package1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
    public byte[] getFile() throws IOException {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("D:\\aaa.log");
            byte[] result = new byte[fileInputStream.available()];
            fileInputStream.read(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }

        }

        return null;
    }
   
    public void copyFile(byte[] result) throws IOException {
        FileOutputStream fileOutputStream = null;
        try {
            File newFile = new File("C:\\copyFile.log");
            fileOutputStream = new FileOutputStream(newFile);
            fileOutputStream.write(result);
            fileOutputStream.write("复制成功了".getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }

    }
   
    public static void main(String[] args) {
        CopyFile copyFile = new CopyFile();
        byte[] result;
        try {
            result = copyFile.getFile();
            copyFile.copyFile(result);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}


你可能感兴趣的:(java,文件copy)