1. 程式人生 > >FILE資料夾建立與刪除及檔案讀取

FILE資料夾建立與刪除及檔案讀取

1.資料夾操作

//新建資料夾目錄
 public static void newFolder(String folderPath)
  {
   String filePath = folderPath.toString();   
   java.io.File myFilePath = new java.io.File(filePath);
   try
   {
    if(myFilePath.isDirectory())
    {
     System.out.println("the directory is exists!");
    }
    else
    {
     myFilePath.mkdir();
     System.out.println("新建目錄成功");
    }
   }
   catch(Exception e)
   {
    System.out.println("新建目錄操作出錯");
    e.printStackTrace();
   }
 }

//級聯刪除檔案
     public static boolean deleteFolder(String _filePath)    
     { 
    java.io.File folder = new java.io.File(_filePath);
         boolean  result  =   false ;
         try 
         {
             
          String childs[] = folder.list(); 
             if(childs == null || childs.length <= 0)    
             { 
                  if (folder.delete())
                  {
                      result  =   true ;
                  }
             }  
             else 
             {
                   for( int i = 0 ; i < childs.length; i ++ )    
                   { 
                          String  childName = childs[i]; 
                          String  childPath = folder.getPath() + File.separator + childName; 
                          File  filePath = new File(childPath); 
                           if(filePath.exists() && filePath.isFile())    
                           {
                                 if (filePath.delete()) 
                                 {
                                    result  =   true ;
                                 }
                                 else 
                                 {
                                    result  =   false ;
                                    break ;
                                 }
                          }  
                          else if(filePath.exists() && filePath.isDirectory())    
                          { 
                                if (deleteFolder(filePath.toString())) 
                                {
                                    result  =   true ;
                                }
                                else 
                                {
                                    result  =   false ;
                                     break ;
                                }
                          }  
                   }
             }
          folder.delete(); 
          } catch (Exception e) 
          {
              e.printStackTrace();
              result  =   false ;
          }
          return  result;
    }

2.檔案讀取

  File fr = null;
  fr = new File("c:/test/" + "abcdet" + ".txt");// 建立FileReader物件,並例項化為fr
  InputStreamReader isr;
  try {
   isr = new InputStreamReader(new FileInputStream(fr), "GBK");//處理中文亂碼
   BufferedReader br = new BufferedReader(isr);
   String line = null;

   line = br.readLine();//按行讀取
   // 從檔案讀取一行字串
   int number=0;
         while (line != null) {// 判斷讀取到的字串是否不為空
    number+=1;
    
    System.out.println(line);
    line=br.readLine();
   }
   System.out.println(number);
     } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }// 建立BufferedReader物件,並例項化為br