java --文件操作

工程做完了,总结下用到的文件操作

package com.cheqiren.caren.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**    
程序名     FileUtils.java
程序功能    文件/文件夹 创建及删除
作成者                  xxx
作成日期    2015-11-06
======================修改履历======================
项目名                    状态            作成者        作成日期
--------------------------------------------------
caren                新规            xxx        2015-11-06
=================================================
*/

public class FileUtils {
    
    /** 
    * 删除单个文件 
    * @param   sPath    被删除文件的文件名 
    * @return 单个文件删除成功返回true,否则返回false 
    */  
   public static boolean deleteFile(String sPath) throws Exception {
       File file = new File(sPath);
       // 路径为文件且不为空则进行删除 
       if (file.isFile() && file.exists()) {
           file.delete();
       }
       return true;
   }
   
   /**
    * 判断文件是否存在
    * @param sPath 文件全路径
    * @return
    * @throws Exception
    */
   public static boolean fileIsExists(String sPath) throws Exception {
       File file = new File(sPath);
       return file.isFile() && file.exists();
   } 
   
   /**
    * 创建文件夹路径
    * (给定全路径,该方法逐级创建文件夹)
    * @param  sPath
    *             文件夹路径
    * @return 创建成功返回true,否则返回false 
    */
   public static boolean creatFolder(String sPath) throws Exception {
       File firstFolder;
       String[] pathArr = sPath.split("/");
       String checkPath = "";
       for(String subPath : pathArr){
           checkPath += subPath + "\\";
           firstFolder = new File(checkPath);
           if(!firstFolder.exists() && !firstFolder.isDirectory()) {
               System.out.println("//文件夹不存在" + checkPath);
               firstFolder.mkdir();
           }else{
               System.out.println("//文件夹存在" + checkPath);
           }
       }
       
       return true;
   }
   
   /**
    * 将文件内容解析位字符串
    * @param storePath 文件路径
    * @return 内容
    * @throws Exception
    */
   public static String readContentFromDisk(String storePath) throws Exception{
       StringBuffer content = new StringBuffer();
       
       File file = new File(storePath);
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = reader.readLine()) != null){
                content.append(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return content.toString();
   }
   
   /**
    * 将内容写入文件
    * @param storePath 文件路径
    * @param content 写入内容
    * @return 执行结果
    */
   public static boolean wirteContentToDisk(String storePath, String content) {

        BufferedWriter writer = null;
        FileWriter fileWriter = null;
        File file = null;
        try {
            file = new File(storePath);
            fileWriter = new FileWriter(file);
            writer = new BufferedWriter(fileWriter);
            writer.write(content);
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }
}


转载于:https://my.oschina.net/u/2312022/blog/603385

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值