乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      在Java實(shí)現(xiàn)Dos中的文件操作命令功能

       Joshua 2006-01-17
      這幾天要對(duì)項(xiàng)目進(jìn)行修改,主要是添加對(duì)文件操作的功能。如,刪除一條信息后要同時(shí)刪除它所帶的附件。于是在空閑時(shí)間,決定把功能擴(kuò)大一下,做一套用Java實(shí)現(xiàn)的Dos命令的簡(jiǎn)單版本。Java的IO中,要對(duì)文件進(jìn)行操作用的是class File(InputStream之類(lèi)的我把它們視為是對(duì)文件內(nèi)容進(jìn)行操作的class)。所以,我們要用的的操作都是通過(guò)File這個(gè)類(lèi)來(lái)實(shí)現(xiàn)的。下面,我們將實(shí)現(xiàn)所要的功能。
      一.    File Class
      1.    class File對(duì)象描述了所指定路徑下的文件和目錄的信息。通過(guò)File對(duì)象,可以查看某個(gè)路徑下的文件和目錄信息,并可操作路徑下的文件和目錄。
      2.    class File的應(yīng)用
      2.1    查看(查詢)某個(gè)路徑下的文件信息。
      2.1.1    class File的構(gòu)造函數(shù)
      class File有兩類(lèi)構(gòu)造函數(shù),一類(lèi)帶有一個(gè)參數(shù),用于產(chǎn)生一個(gè)描述參數(shù)所所指向的路徑下的文件系統(tǒng)的對(duì)象;另一類(lèi)帶有兩數(shù),第一個(gè)參數(shù)表示路徑,第二個(gè)參數(shù)表示子目錄,用來(lái)產(chǎn)生描述第一個(gè)參數(shù)所指向的路徑下的某個(gè)子目錄的文件信息,子目錄名稱由第二個(gè)參數(shù)指定。如:
      File path = new File(“c:\\”);產(chǎn)生一個(gè)描述c:\下的文件信息的對(duì)象
      File path = new File(“c:\\”, “test”);產(chǎn)生一個(gè)描述c:\的test子目錄的文件信息的對(duì)象。
      2.1.2    list()和list(FilenameFilter filter)函數(shù)
      1)    list()函數(shù)以String數(shù)組的形式返回一個(gè)File對(duì)象所描述的文件信息。返回的是路徑下的所有文件和目錄的文件名和目錄名。
      2)    list(FilenameFilter filter)函數(shù)則提供了查詢功能,通過(guò)它可能查詢出滿足特定條件的文件名和目錄名。
      2.1.3    用FinenameFilter接口進(jìn)行文件或目錄的查詢
      2.1.3.1.    這個(gè)接口只包含一個(gè)函數(shù):boolean accept(File dir, String name),第二個(gè)參數(shù)代表一個(gè)文件或目錄的名稱,第一個(gè)參數(shù)代表文件或目錄所在的目錄的名稱。當(dāng)返回true,表示文件滿足查詢條件,要被放入結(jié)果中;否則,表示文件不滿足要求。
      2.1.3.2.    工作原理
      當(dāng)調(diào)用某個(gè)File對(duì)象的list(FilenameFilter filter)函數(shù)時(shí), 會(huì)對(duì)File對(duì)象中的每一個(gè)文件或目錄調(diào)用參數(shù)filter中的accept()方法。在對(duì)一個(gè)文件或目錄調(diào)用accept()函數(shù)時(shí)把它的名稱以及一個(gè)描述它所在的目錄的File對(duì)象作為參數(shù)傳給accept()函數(shù)。
      2.1.4    一個(gè)簡(jiǎn)單的實(shí)例
      1. import java.io.File;
      2. import java.io.FilenameFilter;
      3. class DirList{
      4.     public void printFile(String dir, String filter){
      5.         File path = new File(dir);
      6.         String[] fileList;
      7.         if(filter.length()==0){
      8.             System.out.println("\nAll file:");
      9.             fileList = path.list(); //取得所有文件信息
      10.         }
      11.         else{
      12.             System.out.println("\nAll file including " + filter);
      13.             //取得滿足查詢條件的文件信息
      14.             fileList = path.list(new DirFilter(filter));
      15.         }
      16.         for(int i=0; i<fileList.length; i++)
      17.             System.out.println(fileList[i]);
      18.     }
      19.     public void printFile(String dir){
      20.         printFile(dir, "");
      21.     }
      22. }
      23. class DirFilter implements FilenameFilter{
      24.     String afn; //存放查詢條件
      25.     DirFilter(String afn) { this.afn = afn; }
      26.     //滿足查詢條件,返回true
      27.     public boolean accept(File dir, String name){
      28.         return name.indexOf(afn)!=-1;
      29.     }
      30. }
      31. public class TestIO{
      32.     public static void main(String[] args){
      33.         DirList dirList = new DirList();
      34.         //顯示F:\test下的文件信息
      35.         dirList.printFile("F:\\test");
      36.         //顯示F:\test下名稱包含“.rar”文件信息
      37.         dirList.printFile("F:\\test"".rar");
      38.         //顯示F:\test下名稱包含“r”文件信息
      39. dirList.printFile("F:\\test""r");
      40.     }
      41. }

      這個(gè)只是一個(gè)簡(jiǎn)單的例子,只要完善accept()函數(shù),你也可以做出一個(gè)Java版的dir命令來(lái)。
      2.2    當(dāng)然,我們還能通過(guò)File來(lái)文件或目錄進(jìn)行創(chuàng)建、刪除和改名的操作。
      1. import java.io.File;
      2. import java.io.FilenameFilter;
      3. import java.util.Date;
      4. import java.text.SimpleDateFormat;
      5. class DirFilter implements FilenameFilter{
      6.     String afn;
      7.     DirFilter(String afn) { this.afn = afn; }
      8.     public boolean accept(File dir, String name){
      9.         return name.indexOf(afn)!=-1;
      10.     }
      11. }
      12. class OptFile{
      13.     public static String getAbsolutePath(File f){
      14.         return f.getAbsolutePath();
      15.     }
      16.     public static String getPath(File f){
      17.         return f.getParent();
      18.     }
      19.     public static String getName(File f){
      20.         return f.getName();
      21.     }
      22.     public static long getLength(File f){
      23.         return f.length();
      24.     }
      25.     public static String getParent(File f){
      26.         return f.getParent();
      27.     }
      28.     public static String getLastModified(File f, String format){
      29.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      30.         if(format.equals("ymdh"))
      31.             sdf = new SimpleDateFormat("yyyy-MM-dd hh");
      32.         else if(format.equals("ymdhm"))
      33.             sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
      34.         return sdf.format((new Date(f.lastModified())));
      35.     }
      36.     public static String getLastModified(File f){
      37.         return getLastModified(f, "ymd");
      38.     }
      39.     public static boolean canRead(File f){
      40.         return f.canRead();
      41.     }
      42.     public static boolean canWrite(File f){
      43.         return f.canWrite();
      44.     }
      45.     public static boolean isFile(File f){
      46.         return f.isFile();
      47.     }
      48.     public static boolean isDir(File f){
      49.         return f.isDirectory();
      50.     }
      51.     public static boolean rename(File oldName, File newName){
      52.         return oldName.renameTo(newName);
      53.     }
      54.     public static boolean delete(File f){
      55.         return f.delete();
      56.     }
      57.     public static boolean mkdir(File f){
      58.         return f.mkdirs();
      59.     }
      60.     /**
      61.      * 從指定路徑中查詢文件
      62.      */
      63.     public static File[] findFile(String filePath, String fileName){
      64.         File f = new File(filePath);
      65.         File[] result = f.listFiles(new DirFilter(fileName));
      66.         return result;
      67.     }
      68.     public static void printFileAtt(File f){       
      69.         System.out.println(
      70.             " Absolute path: " + getAbsolutePath(f) +
      71.             "\n Can read: " + canRead(f) +
      72.             "\n Can write: " + canWrite(f) + 
      73.             "\n path: " + getPath(f) +
      74.             "\n name: " + getName(f) +
      75.             "\n parent: " + getParent(f) +
      76.             "\n length: " + getLength(f) +
      77.             "\n lastModified: " + getLastModified(f));
      78.         if(isFile(f))
      79.             System.out.println(" it‘s a file");
      80.         else if(isDir(f))
      81.             System.out.println(" it‘s a directory");
      82.     }
      83. }
      84. public class TestIO{
      85.     public static void main(String[] args){
      86.         File f1 = new File("F:\\nepalon\\thinkinginjava\\test");
      87.         File f2 = new File("F:\\nepalon\\thinkinginjava\\test\\aa ");
      88.         OptFile.printFileAtt(f1);
      89.         OptFile.printFileAtt(f2);
      90.         //當(dāng)對(duì)文件或目錄進(jìn)行改名,且更新到一個(gè)不同的下時(shí),
      91.         //會(huì)先把文件或整個(gè)目錄剪切到新目錄下,再重新命名
      92.         File nf2 = new File("F:\\nepalon\\thinkinginjava\\使用log4j.files");
      93.         if(OptFile.rename(f2, nf2)){
      94.             System.out.println("after rename f2:");
      95.             OptFile.printFileAtt(nf2);
      96.         }
      97.         else
      98.             System.out.println("rename nf2 failly");
      99.       //進(jìn)行刪除操作時(shí),只能刪除文件;如果刪除的是目錄,會(huì)返回false
      100.         File df = new File("F:\\nepalon\\thinkinginjava\\test\\1001.doc");
      101.         if(OptFile.delete(df))
      102.             System.out.println("Delete df successfully");
      103.         else
      104.             System.out.println("Delete df failly");
      105.         System.out.println("Find file with name \"1001\"");
      106.         OptFile.findFile("F:\\nepalon\\thinkinginjava\\test""1001");
      107.         File mf = new File("F:\\nepalon\\thinkinginjava\\test\\1001");
      108.         if(OptFile.mkdir(mf))
      109.             System.out.println("create directory mf successfully");
      110.         else
      111.             System.out.println("create directory mf failly");
      112.     }
      113. }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類(lèi)似文章 更多