Java文件的拷貝
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;/**
- 文件的操作
@author Administrator / public class FileUtil {
/**
- 文件的拷貝
- @param srcPath 源文件的路徑
- @param destPath 目標文件路徑
@throws Exception */
public static void copyFile(String srcPath,String destPath) throws Exception{
copyFile(new File(srcPath),new File(destPath));
}
/**
- 文件的拷貝
- @param src 源文件的File對象
- @param dest 目標文件的File對象
@throws IOException */ public static void copyFile(File src,File dest) throws IOException{
if(!src.isFile()){
System.out.println("只能拷貝文件!!"); throw new IOException("只能拷貝文件!!");
} //dest為已經存在的文件夾,不能建立于文件夾同名的文件 if(dest.isDirectory()){
System.out.println("不能建立于文件夾同名的文件"); throw new IOException("不能建立于文件夾同名的文件");
} //選擇流 InputStream is = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); //文件的拷貝 循環+讀取+寫出 byte[] flush = new byte[1024]; int len=0; //讀取 while((len=(is.read(flush)))!=-1){
//寫出 out.write(flush, 0, len);
} out.flush(); //關閉輸出流 out.close(); is.close(); }
}</pre>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!