java學(xué)習(xí)

          java學(xué)習(xí)

           

          java 中文件的復(fù)制,查詢,刪除

          javaIO中,文件的查詢和刪除,文件的復(fù)制程序如下:

          普通的復(fù)制是:

          public class Acopy {

           public void copy(String oldpath,String  newpath ) throws IOException {

              File of = new File(oldpath);

              File nf = new File(newpath);

              if(!nf.exists()){

                nf.createNewFile();

              }

              FileInputStream i = new FileInputStream(of); 

              FileOutputStream o = new FileOutputStream(nf);

              int b= 0;

              byte[] buffer = new byte[100];

              while((b=i.read(buffer))!=-1){

                o.write(buffer, 0, b-1);

              }

              i.close();

              o.flush();

              o.close();

            }

          }

          加強(qiáng)的復(fù)制是:

          public class Bcopy {

           public void copy(String opath,String npath) throws IOException{

              File of = new File(opath);

              File nf = new File(npath);

              if(!nf.exists()){

                nf.createNewFile();

              }

              FileInputStream i = new FileInputStream(of);

              BufferedInputStream bi = new BufferedInputStream(i);

              FileOutputStream o = new FileOutputStream(nf);

              BufferedOutputStream bo = new BufferedOutputStream(o);

              int b = 0;

              byte[] buffer = new byte[100];

              while((b=bi.read(buffer))!=-1){

                bo.write(buffer, 0, b-1);

              }

              bi.close();

              bo.flush();

              bo.close();

            }

          }

          文件的查詢是:

          public void show(String path){

              File f = new File(path);

              if(f.isFile()){

                System.out.println(f.getPath());

              }else if(f.isDirectory()){

                File[] files = f.listFiles();

                if(files!=null){

                  for(File file : files){

                    if(file.isFile()){

                      System.out.println(file.getPath());

                    }else if(file.isDirectory()){

                      System.out.println("["+file.getPath()+"]");

                      show(file.getPath());

                    }

                  }

                }

              }

            }

          文件的刪除是:

          public void del(String path){

              File f = new File(path);

              if(f.isFile()){

                f.delete();

              }else if(f.isDirectory()){

                File[] files = f.listFiles();

                if(files.length==0){

                  f.delete();

                }else if(files!=null){

                  for(File file : files){

                    if(file.isFile()){

                      file.delete();

                    }else if(file.isDirectory()){

                      del(file.getPath());

                    }

                  }

                }

              }

              f.delete();

            }

          posted on 2011-11-26 12:27 楊軍威 閱讀(295) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 文安县| 乾安县| 大洼县| 金堂县| 商南县| 大足县| 侯马市| 东安县| 喀喇沁旗| 柯坪县| 军事| 桓仁| 仲巴县| 巴青县| 天柱县| 洛川县| 北海市| 颍上县| 石台县| 安国市| 黄平县| 尤溪县| 阳城县| 姚安县| 和龙市| 峨山| 灵山县| 黑龙江省| 城口县| 平山县| 寿宁县| 鲁甸县| 女性| 巩义市| 汕头市| 渭源县| 正宁县| 晋州市| 赞皇县| 呼和浩特市| 台东县|