- 论坛徽章:
- 0
|
文件复制- package file;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- /**
- * 提供java IO 操作
- * @author ronaldoGT
- *
- */
- public class IOfile {
-
- /**
- * @param args
- */
- private static IOfile ioFile = new IOfile();
- private static String oldUrl = "E://FunshionMedia/黑客帝国/老梁观世界-20130311.mp4";
- private static String newUrl = "E://老梁观世界-20130311.mp4";
-
- public static void main(String[] args) {
- ioFile.copyFile(new File(oldUrl),new File(newUrl));
- ioFile.copyDirectory(new File("D:/html5"),"E:/");
- System.out.println("ok!");
- }
-
- /**
- * 复制文件夹,嵌套文件夹,以及包含的文件
- * @param oldDirectory
- * @param newUrl
- */
- public void copyDirectory(File oldDirectory,String newUrl){
- File[] f=oldDirectory.listFiles();
- File file = new File(newUrl);
- String url;
- url = oldDirectory.getName();
- newUrl += "/"+url;
- file = new File(newUrl);
- file.mkdir();
- for(File ff:f){
- if(ff.isDirectory()){
- copyDirectory(ff,newUrl);
- }
- if(ff.isFile()){
- copyFile(ff,new File(newUrl+"/"+ff.getName()));
- }
- }
-
- }
-
-
- /**
- * 复制文件
- * @param oldFile
- * @param newFile
- */
- public void copyFile(File oldFile,File newFile){
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new BufferedInputStream(new FileInputStream(oldFile));
- fos = new BufferedOutputStream(new FileOutputStream(newFile));
- byte[] buf = new byte[1024*2];
- int len ;
- while((len = fis.read(buf)) != -1){
- fos.write(buf, 0, len);
- }
- fos.flush();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- fis.close();
- fos.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- }
复制代码 |
|