zoukankan      html  css  js  c++  java
  • 备份几个文件操作方法

    前阵子写过一个小demo,有几个文件操作下相关的方法,留在这下次直接粘贴复制的

      1 /**
      2      * 将byte数组保存为文件
      3      * 
      4      * @param bfile
      5      *            字节数组
      6      * @param filePath
      7      *            文件路径
      8      * @param fileName
      9      *            文件名
     10      * @author zhangyanhua
     11      * @date 2019年6月5日 下午3:52:50
     12      */
     13     private boolean saveFile(byte[] bfile, String filePath, String fileName)
     14     {
     15         BufferedOutputStream bos = null;
     16         FileOutputStream fos = null;
     17         File file = null;
     18         try
     19         {
     20             File dir = new File(this.getFolder(filePath));
     21             if (!dir.exists() && !dir.isDirectory())
     22             {
     23                 dir.mkdirs();
     24             }
     25             file = new File(filePath + "\" + fileName);
     26             File parentFile = file.getParentFile();
     27             if (!parentFile.exists() && !parentFile.isDirectory())
     28             {
     29                 parentFile.mkdirs();
     30             }
     31 
     32             fos = new FileOutputStream(file);
     33             bos = new BufferedOutputStream(fos);
     34             bos.write(bfile);
     35             return true;
     36         }
     37         catch (Exception e)
     38         {
     39             logger.error("保存文件出错 " + e);
     40             return false;
     41         }
     42         finally
     43         {
     44             if (bos != null)
     45             {
     46                 try
     47                 {
     48                     bos.close();
     49                 }
     50                 catch (IOException e1)
     51                 {
     52                     e1.printStackTrace();
     53                 }
     54             }
     55             if (fos != null)
     56             {
     57                 try
     58                 {
     59                     fos.close();
     60                 }
     61                 catch (IOException e1)
     62                 {
     63                     e1.printStackTrace();
     64                 }
     65             }
     66         }
     67     }
     68 
     69     /**
     70      * 获取conf.properties文件中的一个配置名称对应的值
     71      * 
     72      * @param name
     73      * @return
     74      * @author zhangyanhua
     75      * @date 2019年4月10日 上午10:50:39
     76      */
     77     private String getConfValue(String name)
     78     {
     79         InputStream inStream;
     80         try
     81         {
     82             Properties prop = new Properties();
     83             inStream = new FileInputStream(new File(confPath));
     84             prop.load(inStream);
     85             return prop.getProperty(name);
     86         }
     87         catch (FileNotFoundException e)
     88         {
     89             logger.error("配置文件" + confPath + "不存在!" + e);
     90         }
     91         catch (IOException e)
     92         {
     93             logger.error("初始化Properties失败!" + e);
     94         }
     95         return null;
     96     }
     97 
     98     /**
     99      * 处理目录字符串
    100      * 
    101      * @param folder
    102      * @return
    103      * @author zhangyanhua
    104      * @date 2019年4月11日 上午11:26:13
    105      */
    106     private String getFolder(String folder)
    107     {
    108         if (StringUtils.isEmpty(folder))
    109         {
    110             folder = "";
    111         }
    112         else
    113         {
    114             if (!folder.endsWith("/"))
    115             {
    116                 folder += "/";
    117             }
    118         }
    119         return folder;
    120     }
    121 
    122     /**
    123      * 获取指定目录下的全部文件
    124      * 
    125      * @param fileList
    126      *            文件集合,递归调用
    127      * @param path
    128      *            需要获取文件的目录
    129      * @return
    130      * @author zhangyanhua
    131      * @date 2019年4月10日 上午11:26:16
    132      */
    133     private List<File> getFileList(List<File> fileList, String path)
    134     {
    135         if (StringUtils.isEmpty(path))
    136         {
    137             logger.error("目标文件不存在或者文件夹是空的!");
    138             return null;
    139         }
    140 
    141         File file = new File(path);
    142         if (file.exists())
    143         {
    144             File[] files = file.listFiles();
    145             if (null == files || files.length == 0)
    146             {
    147                 logger.error("目标文件不存在或者文件夹是空的!");
    148                 return null;
    149             }
    150             else
    151             {
    152                 for (File file1 : files)
    153                 {
    154                     if (file1.isDirectory())
    155                     {
    156                         // 获取文件绝对路径
    157                         getFileList(fileList, file1.getAbsolutePath());
    158                         continue;
    159                     }
    160                     else
    161                     {
    162                         fileList.add(file1);
    163                     }
    164                 }
    165             }
    166         }
    167         else
    168         {
    169             logger.error("目标文件不存在!");
    170         }
    171         return fileList;
    172     }
  • 相关阅读:
    004---基于TCP的套接字
    003---socket介绍
    002---tcp/ip五层详解
    001---C/S架构
    008---re正则模块
    007---logging日志模块
    006---hashlib模块
    005---json & pickle
    004---os & sys
    22.解决 eclipse 与 AS 共用 SDK 导致 eclipse ADT 无法使用的问题
  • 原文地址:https://www.cnblogs.com/yanh0606/p/10983103.html
Copyright © 2011-2022 走看看