前陣子寫過一個小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 }