<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 33,  comments - 17,  trackbacks - 0
      1// 文件拷貝
      2import java.io.*
      3import java.util.ArrayList; 
      4import java.util.List; 
      5public class FileCopy 
      6private String message = ""
      7public String getMessage() 
      8  return message; 
      9}
     
     10public void setMessage(String message) 
     11  this.message = message; 
     12}
     
     13/** 
     14  * 將源文件拷貝到目標(biāo)文件 
     15  * 
     16  * @param src 
     17  *            寫源文件地址,需文件名 
     18  * @param des 
     19  *            寫目標(biāo)文件地址,無需文件名 
     20  */
     
     21public boolean copyFile(String src, String des) 
     22  File srcFile = new File(src); 
     23  File desDir = new File(des); 
     24  File desFile = new File(des + "/" + srcFile.getName()); 
     25  // 判斷源文件是否存在 
     26  if (!srcFile.exists()) 
     27   this.setMessage("源文件不存在!"); 
     28   return false
     29  }
     else if (!srcFile.isFile()) 
     30   this.setMessage("源文件格式錯(cuò)!"); 
     31   return false
     32  }
     
     33  // 判斷源文件是否存在 
     34  if (!desDir.exists()) 
     35   this.setMessage("目標(biāo)目錄不存在!"); 
     36   return false
     37  }
     else if (!desDir.isDirectory()) 
     38   this.setMessage("不是有效的目錄!"); 
     39   return false
     40  }
     
     41  BufferedReader reader = null
     42  BufferedWriter writer = null
     43  String str; 
     44  try 
     45   reader = new BufferedReader(new FileReader(srcFile)); 
     46   writer = new BufferedWriter(new FileWriter(desFile)); 
     47   // 判斷目標(biāo)文件是否存在及其格式,不存在就創(chuàng)建,格式不對(duì)先刪除,存在就替代 
     48   if (!desFile.exists() || !desFile.isFile()) 
     49    if (desFile.exists()) 
     50     desFile.delete(); 
     51    }
     
     52    desFile.createNewFile(); 
     53   }
     
     54   // 從源文件讀取數(shù)據(jù),并寫入目標(biāo)文件 
     55   str = reader.readLine(); 
     56   while (str != null
     57    writer.write(str); 
     58    writer.newLine(); 
     59    str = reader.readLine(); 
     60   }
     
     61  }
     catch (IOException e) 
     62   this.setMessage(e.getMessage()); 
     63   return false
     64  }
     finally 
     65   if (reader != null
     66    try 
     67     reader.close(); 
     68    }
     catch (IOException e) 
     69     this.setMessage(e.getMessage()); 
     70    }
     
     71   }
     
     72   if (writer != null
     73    try 
     74     writer.close(); 
     75    }
     catch (IOException e) 
     76     this.setMessage(e.getMessage()); 
     77    }
     
     78   }
     
     79  }
     
     80  return true
     81}
     
     82private List fileList = new ArrayList(); 
     83
     84/** 
     85  * 列出所有文件 
     86  * @param srcFile 
     87  */
     
     88private void file(File srcFile) 
     89  if (srcFile.isDirectory()) 
     90   String[] files = srcFile.list(); 
     91   
     92   for (int i = 0; i < files.length; i++
     93    File f = new File(srcFile + "/" + files[i]); 
     94    // 如果是文件加入列表,否則遞歸列出 
     95    if (f.isFile()) 
     96     fileList.add(f); 
     97    }
     else 
     98     file(f); 
     99   }
     
    100  }
    else this.setMessage(srcFile.getAbsolutePath()+"不是目錄"); 
    101}
     
    102/** 
    103  * 建立目錄 
    104  * @param des 
    105  * @throws IOException 
    106  */
    private void mkdir(File des) 
    107  if (!des.exists() || !des.isDirectory()) 
    108   mkdir(des.getParentFile()); 
    109   if (des.exists()) 
    110    des.delete(); 
    111   }
     
    112   des.mkdir(); 
    113  }
     
    114}
     
    115/** 
    116  * 復(fù)制目錄  將源目錄下所有文件拷貝到目標(biāo)目錄下 
    117  * @param src  源目錄 
    118   * @param des  目標(biāo)目錄 
    119  */
     
    120public boolean copyDir(String src, String des) 
    121  File srcFile = new File(src); 
    122  if (!srcFile.exists()) 
    123   this.setMessage("源目錄不存在!"); 
    124   return false
    125  }
     else if (!srcFile.isDirectory()) 
    126   this.setMessage(src+"不是有效的目錄!"); 
    127   return false
    128  }
     
    129  file(srcFile); 
    130   
    131  for (int i = 0; i < fileList.size(); i++
    132   String srcName = ((File) fileList.get(i)).getPath(); 
    133   String desName = srcName.substring(src.length(), srcName.length()); 
    134   desName = des + desName; 
    135   File dir=new File(desName).getParentFile(); 
    136   mkdir(dir); 
    137   
    138   if(!copyFile(srcName, dir.getPath()))
    139    return false
    140   }
     
    141  }
     
    142  return true
    143}
     
    144public static void main(String[] args) 
    145
    146  FileCopy t = new FileCopy(); 
    147  System.out.println(t.copyFile("D:/aaa.txt","E:")); 
    148  String src="D:/asdf"
    149  String des="E:/adf"
    150  System.out.println(t.copyDir(src, des)); 
    151  System.out.println(t.getMessage()); 
    152}
     
    153
    154}

    155
    156
    posted on 2008-07-23 17:41 scea2009 閱讀(83) 評(píng)論(0)  編輯  收藏

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


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

    <2008年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    PL/SQL存儲(chǔ)過程與函數(shù)

    搜索

    •  

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 91免费在线播放| 亚洲av无码专区国产不乱码| 国产亚洲精久久久久久无码| 亚洲乱码中文字幕手机在线| 免费一级特黄特色大片在线观看 | 亚洲精品夜夜夜妓女网| 亚洲精品国产高清嫩草影院 | a级毛片在线免费观看| 久久er国产精品免费观看8| 人碰人碰人成人免费视频| 一级黄色免费大片| 一级特黄特色的免费大片视频| 国产亚洲综合视频| 一二三四在线观看免费中文在线观看| 精品视频免费在线| j8又粗又长又硬又爽免费视频| 本道天堂成在人线av无码免费| 中国videos性高清免费| 国产做国产爱免费视频| 男女午夜24式免费视频| 一区二区三区四区免费视频| 4399影视免费观看高清直播| 亚洲综合激情六月婷婷在线观看| 亚洲精品视频免费在线观看| 亚洲一级毛片在线播放| 亚洲欧美日韩国产精品一区| 国产亚洲综合久久| 国产成人1024精品免费| 香蕉成人免费看片视频app下载| 97久久免费视频| 三上悠亚电影全集免费| 久久精品国产这里是免费| 18未年禁止免费观看| 毛片免费观看的视频| 国产一区在线观看免费| 亚洲日本乱码在线观看| 亚洲国产高清在线精品一区| 亚洲精华国产精华精华液 | 国产精品美女自在线观看免费 | 1000部国产成人免费视频| 国产无遮挡裸体免费视频 |