神秘的 J2ee 殿堂

          ·古之學者必有師·做學者亦要做師者·FIGHTING·

          Java文件操作小記

          Java文件操作小記

          Test.java
          package com.hunau.liuyong;
          import com.hunau.liuyong.FileMethod;
          public class Test{
          public
           static void main(String[] args) throws IOException
          {
          FileMethod fileMethod = new FileMethod();
          String[] ss = new String[50];
          for(int i=0;i<ss.length;i++)
          {
          ss[i] 
          = "網(計算機軟硬件,通訊) "+i;
          }
          System.out.println(
          "f:\\dd\\");
          fileMethod.fileIsNull("f:\\ss\\","me.txt");
          }
          }
          FileMethod.java
          package com.hunau.liuyong;
          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileReader;
          import java.io.FileWriter;
          import java.io.IOException;
          import java.io.PrintWriter;
          public class FileMethod{

              
          /**
               * 
               * 文件的寫入
               * 
               * 
          @param filePath(文件路徑)
               * 
               * 
          @param fileName(文件名)
               * 
               * 
          @param args[]
               * 
               * 
          @throws IOException
               * 
               
          */

              
          public void writeFile(String filePath, String fileName, String[] args)
                      
          throws IOException {
                  //1
                  FileWriter fw 
          = new FileWriter(filePath + fileName);
                  //2   1處可以用2處代替
                  //File fw = new File(filePath+fileName);
                  //fw.createNewFile();
                  PrintWriter out 
          = new PrintWriter(fw);
                  
          for (int i = 0; i < args.length; i++) {
                      out.write(args[i]);
                      out.println();
                      out.flush();
                  }
                  fw.close();
                  out.close();
              }

              
          /**
               * 
               * 文件的寫入
               * 
               * 
          @param filePath(文件路徑)
               * 
               * 
          @param fileName(文件名)
               * 
               * 
          @param args
               * 
               * 
          @throws IOException
               * 
               
          */

              
          public void writeFile(String filePath, String fileName, String args)
                      
          throws IOException

              {
                  FileWriter fw 
          = new FileWriter(filePath + fileName);
                  fw.write(args);
                  fw.close();

              }

              
          /**
               * 
               * 創建與刪除文件
               * 
               * 
          @param filePath
               * 
               * 
          @param fileName
               * 
               * 
          @return 創建成功返回true
               * 
               * 
          @throws IOException
               * 
               
          */

              
          public boolean createAndDeleteFile(String filePath, String fileName)
                      
          throws IOException {
                  
          boolean result = false;
                  File file 
          = new File(filePath, fileName);
                  
          if (file.exists()) {
                      file.delete();
                      result 
          = true;
                      System.out.println(
          "文件已經刪除!");
                  } 
          else {
                      file.createNewFile();
                      result 
          = true;
                      System.out.println(
          "文件已經創建!");
                  }
                  
          return result;
              }

              
          /**
               * 
               * 創建和刪除目錄
               * 
               * 
          @param folderName
               * 
               * 
          @param filePath
               * 
               * 
          @return 刪除成功返回true
               * 
               
          */

              
          public boolean createAndDeleteFolder(String folderName, String filePath)

              {
                  
          boolean result = false;
                  
          try {
                      File file 
          = new File(filePath + folderName);
                      
          if (file.exists()) {
                          file.delete();
                          System.out.println(
          "目錄已經存在,已刪除!");
                          result 
          = true;
                      } 
          else {
                          file.mkdir();
                          System.out.println(
          "目錄不存在,已經建立!");
                          result 
          = true;
                      }
                  } 
          catch (Exception ex) {
                      result 
          = false;
                      System.out.println(
          "CreateAndDeleteFolder is error:" + ex);
                  }
                  
          return result;
              }

              
          /**
               * 
               * 輸出目錄中的所有文件及目錄名字
               * 
               * 
          @param filePath
               * 
               
          */

              
          public void readFolderByFile(String filePath) {
                  File file 
          = new File(filePath);
                  File[] tempFile 
          = file.listFiles();
                  
          for (int i = 0; i < tempFile.length; i++) {
                      
          if (tempFile[i].isFile()) {
                          System.out.println(
          "File : " + tempFile[i].getName());
                      }
                      
          if (tempFile[i].isDirectory()) {
                          System.out.println(
          "Directory : " + tempFile[i].getName());
                      }
                  }
              }

              
          /**
               * 
               * 檢查文件中是否為一個空
               * 
               * 
          @param filePath
               * 
               * 
          @param fileName
               * 
               * 
          @return 為空返回true
               * 
               * 
          @throws IOException
               * 
               
          */

              
          public boolean fileIsNull(String filePath, String fileName)
                      
          throws IOException {
                  
          boolean result = false;
                  FileReader fr 
          = new FileReader(filePath + fileName);
                  
          if (fr.read() == -1) {
                      result 
          = true;
                      System.out.println(fileName 
          + " 文件中沒有數據!");
                  } 
          else {
                      System.out.println(fileName 
          + " 文件中有數據!");
                  }
                  fr.close();
                  
          return result;
              }

              /**
               * 
               * 一行一行的讀取文件中的數據
               * 
               * 
          @param filePath
               * 
               * 
          @param fileName
               * 
               * 
          @throws IOException
               * 
               
          */

              
          public void readLineFile(String filePath, String fileName)
                      
          throws IOException {
                  FileReader fr 
          = new FileReader(filePath + fileName);
                  BufferedReader br 
          = new BufferedReader(fr);
                  String line 
          = br.readLine();
                  
          while (line != null) {
                      System.out.println(line);
                      line 
          = br.readLine();
                  }
                  br.close();
                  fr.close();
              }
          }


          posted on 2008-07-03 10:54 月芽兒 閱讀(292) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 马山县| 郸城县| 双牌县| 吴江市| 上栗县| 武胜县| 怀集县| 米易县| 紫金县| 尚志市| 庆元县| 香港| 紫阳县| 翁源县| 洪洞县| 富源县| 阳高县| 成武县| 麻阳| 福鼎市| 凭祥市| 德钦县| 阿拉善盟| 金寨县| 巨野县| 郧西县| 嘉禾县| 和硕县| 前郭尔| 泽库县| 阿图什市| 循化| 永福县| 揭西县| 兴安县| 凤城市| 酒泉市| 田林县| 阳泉市| 麻栗坡县| 监利县|