神秘的 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 月芽兒 閱讀(293) 評論(0)  編輯  收藏 所屬分類: J2EE學習摘錄

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鲁山县| 泽州县| 三穗县| 洛隆县| 新津县| 石城县| 车致| 济宁市| 斗六市| 琼中| 汉源县| 辉县市| 长兴县| 界首市| 武乡县| 沙田区| 贺州市| 萝北县| 宿迁市| 吴忠市| 永丰县| 嘉义县| 旌德县| 巨鹿县| 永兴县| 壶关县| 汤原县| 偃师市| 大关县| 思茅市| 布尔津县| 黑河市| 兰西县| 拜泉县| 双鸭山市| 汉中市| 刚察县| 凯里市| 分宜县| 浙江省| 滦南县|