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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值