java學習

          java學習

           

          java 中文件的復制,查詢,刪除

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

          普通的復制是:

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

            }

          }

          加強的復制是:

          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) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鄄城县| 宁远县| 磴口县| 龙山县| 顺昌县| 股票| 即墨市| 绥滨县| 潍坊市| 宁津县| 拉萨市| 基隆市| 大洼县| 金门县| 滁州市| 巴彦县| 阿巴嘎旗| 辛集市| 卓尼县| 蓝山县| 达拉特旗| 邯郸市| 五华县| 辽宁省| 琼中| 镇平县| 长汀县| 凤山市| 麻城市| 荃湾区| 温州市| 电白县| 乌鲁木齐市| 喀喇| 宣化县| 青冈县| 南昌市| 南召县| 兴安县| 武胜县| 色达县|