`

java实现文件copy

    博客分类:
  • java
阅读更多

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();
        }
        
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics