欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 文件夹操作,java文件夹,import java.

来源: javaer 分享于  点击 38119 次 点评:198

java 文件夹操作,java文件夹,import java.


import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.nio.ByteBuffer;  import java.nio.channels.FileChannel;  public class FileManipulationTest {      public static void main(String[] args) {          // TODO Auto-generated method stub          File src = new File("C:/Users/leo/Desktop/yidu");          File dst = new File("C:/Users/leo/Desktop/dd/mm");          copy(src, dst,true,true);//剪切      }      public static boolean rename(File file,String name){          String str = file.getParent();          if(!str.endsWith(File.separator))              str += File.separator;          return file.renameTo(new File(str+name));      }      public static void delete(File file){          if(file.isFile()){              file.delete();          }else if(file.isDirectory()){              File[] list = file.listFiles();              for(int i=0;i<list.length;i++){                 delete(list[i]);                              }              file.delete();          }      }      @Deprecated     public static void cut(File src,File dst){          copy(src, dst,true,false);          delete(src);      }  //上面的cut方法将文件夹全部复制后再删除整个文件夹,这种方法不好。因为如果有几个文件复制失败,源文件也会都被删除了  //若要剪切应该用下面的copy方法,将参数cut设为true,它是复制一个删除一个,复制失败就不会删除源文件      /**       *        * @param src:源文件(夹)       * @param dst:目标文件(夹)       * @param forced:如果遇到同名文件,是否覆盖       * @param cut:复制完是否删除源文件,若设为true,效果如同剪切           * 注意:dst是目标路径而不是目标路径所在的文件夹,比如要不c:\dir复制到d:\盘下,则dst应该是File("d:\dir")而不是File("d:\")       */      public static void copy(File src,File dst,boolean forced,boolean cut){          if(src.isFile()){              if(!dst.isFile() || forced)                  try {                      dst.createNewFile();                      _copy(src, dst);                      if(cut)                          src.delete();                  } catch (IOException e) {                      // TODO Auto-generated catch block                      e.printStackTrace();                  }                     }          else if(src.isDirectory()){              dst.mkdirs();              File[] list = src.listFiles();              for(int i=0;i<list.length;i++){                  String rp = list[i].getAbsolutePath().substring(src.getAbsolutePath().length(), list[i].getAbsolutePath().length());                  File dstFile = new File(dst.getAbsolutePath()+rp);                  copy( list[i],dstFile,forced,cut);                            }              if(cut)                  src.delete();          }      }      private static void _copy(File src,File dst) throws IOException{          FileChannel dstfc = null;          FileChannel srcfc = null;          try {              dstfc = new FileOutputStream(dst).getChannel();              srcfc = new FileInputStream(src).getChannel();              ByteBuffer buf = ByteBuffer.allocate(4*1024);              while(srcfc.size()>srcfc.position()){                  buf.clear();                  srcfc.read(buf);                  buf.flip();                  dstfc.write(buf);              }          } finally {              try {                  if(dstfc != null)                      dstfc.close();              } catch (IOException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();              }              try {                  if(srcfc != null)                      srcfc.close();              } catch (IOException e) {                  // TODO Auto-generated catch block                  e.printStackTrace();              }          }      }  } 
相关栏目:

用户点评