常用IO方法

public class FileUtil {

    static Logger log = Logger.getLogger(FileUtil.class);
    
    public static String readFile(InputStream is) throws IOException{
        
        if(is==null) return null;
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        
        String tmp = "";
        String result = "";
        
        try {
            while((tmp = reader.readLine()) !=null)
                if(tmp.length() > 0)
                    result = result + tmp.trim();
        }
        catch (IOException e) {
           throw e;
        }
        finally {
            reader.close();
        }
        
        return result;
    }
    
    public static String readFile(String path) throws IOException{
        
        InputStream is = null;
        
        try {
            is = new FileInputStream(path);
            return readFile(is);
        }
        catch (IOException e) {
            throw e;
        }
        finally {
            is.close();
        }
   
    }
    
    public static boolean removeFile(String path){
        
        return removeFile(path, true);
 
    }
    
    public static boolean removeFile(String filePath, boolean removeParent){  
        
        boolean isdelete = false;
        File file = new File(filePath);
        if(file.exists()){
            isdelete = file.delete();
            
            if(removeParent){
                File parent = file.getParentFile();
                if(parent!=null){
                    if(parent.listFiles()==null || parent.listFiles().length==0){
                        removeFile(parent.getPath(), removeParent);
                    }
                }
            }
        }
        return isdelete;
        
    }
        
    public static void writeFile(String path, String content) throws IOException{
        
        File file = new File(path);
        FileWriter fw = null;
        try {
            fw = new FileWriter(file, true);
            fw.write(content);
            fw.flush();
        }
        catch (IOException e) {
            throw e;
        }
        finally{
            fw.close();
        }
    
    }
      
    public static boolean copyFile(String filePath, String backupPath) throws IOException{
        
        BufferedInputStream reader = null;
        PrintStream out = null;
            
        File file = new File(filePath);
        if(file.exists()){
            
            try {
                reader = new BufferedInputStream(new FileInputStream(file));

                String directory = backupPath.substring(0, backupPath.lastIndexOf("/") + 1);
                
                File tempfile = new File(directory);
                if(!tempfile.exists()){
                    tempfile.mkdirs();
                }
                
                File backupFile = new File(backupPath);
                out = new PrintStream(new BufferedOutputStream(new FileOutputStream(backupFile)));
     
                byte[] b = new byte[2048];
                
                while((reader.read(b, 0, b.length))!=-1){
                    out.write(b);
                }
                out.flush();
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            finally {          
                reader.close();
                out.close();
            }

        } else {
            log.error("文件" + filePath + "在当前路径下不存在!");
            return false;
        }        
        return true;
        
    }
    
}

你可能感兴趣的:(IO)