常用文件(夹)处理方法工具类
来源:互联网 发布:纳什职业生涯总数据 编辑:程序博客网 时间:2024/04/28 19:09
功能:文件夹创建、文件删除、文件保存和读取、文件压缩与解压缩、excel文件打印
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.OutputStream; 10 import java.io.RandomAccessFile; 11 import java.io.Reader; 12 import java.nio.MappedByteBuffer; 13 import java.nio.channels.FileChannel; 14 import java.nio.channels.FileChannel.MapMode; 15 import java.text.DecimalFormat; 16 import java.util.Enumeration; 17 import java.util.Properties; 18 import java.util.zip.ZipEntry; 19 import java.util.zip.ZipFile; 20 import java.util.zip.ZipOutputStream; 21 22 import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager; 23 import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication; 24 import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook; 25 import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks; 26 import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet; 27 28 import org.apache.commons.io.FileUtils; 29 30 import com.jacob.activeX.ActiveXComponent; 31 import com.jacob.com.ComThread; 32 import com.jacob.com.Dispatch; 33 import com.jacob.com.Variant; 34 35 /** 36 * 常用文件(夹)处理方法工具类 37 */ 38 public class FileUtil { 39 40 /** 41 * 创建文件夹 42 * @param path 文件夹路径 43 */ 44 public static void mkdirs(String path){ 45 File folder = new File(path); 46 if(!folder.exists()){ 47 folder.mkdirs(); 48 } 49 } 50 51 /** 52 * 删除文件夹下的所有文件 53 * @param rootPath 文件夹路径 54 * @return 删除是否成功 55 */ 56 public static boolean deleteAll(String rootPath){ 57 try { 58 File f = new File(rootPath); 59 String[] files = f.list(); 60 //判断文件夹是否存在文件 61 if(files != null && files.length != 0){ 62 for(int i = 0; i < files.length; i++) { 63 File file = new File(rootPath + files[i]); 64 if(file.exists()) 65 FileUtils.forceDelete(file); 66 } 67 } 68 return true; 69 } catch (IOException e) { 70 e.printStackTrace(); 71 return false; 72 } 73 } 74 75 /** 76 * 删除单个文件 77 * @param fileName 要删除的文件的文件名 78 * @return 删除是否成功 79 */ 80 public static boolean deleteFile(String fileName) { 81 File file = new File(fileName); 82 // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 83 if (file.exists() && file.isFile()) { 84 if (file.delete()) { 85 return true; 86 } else { 87 return false; 88 } 89 } else { 90 return false; 91 } 92 } 93 94 /** 95 * 保存输入流中的文件 96 * @param inputStream 输入流 97 * @param outFilePath 保存的文件路径 98 * @return 保存是否成功 99 */ 100 public static boolean saveFileFromInputStream(InputStream inputStream,String outFilePath) { 101 try{102 FileOutputStream fileOutputStream = new FileOutputStream(outFilePath);103 byte[] buffer =new byte[1024*1024]; 104 int byteread = 0; 105 while ((byteread=inputStream.read(buffer))!=-1) 106 { 107 fileOutputStream.write(buffer,0,byteread); 108 fileOutputStream.flush(); 109 } 110 fileOutputStream.close(); 111 inputStream.close(); 112 return true;113 }catch(Exception e){114 return false;115 }116 } 117 118 /** 119 * 以行为单位读取文件,常用于读面向行的格式化文件 120 * @param fileName 文件的路径121 * @return 122 */ 123 public static String readFileByLines(String fileName) { 124 File file = new File(fileName); 125 BufferedReader reader = null; 126 StringBuffer buf = new StringBuffer();127 try { 128 reader = new BufferedReader(new FileReader(file)); 129 String tempString = null; 130 // 一次读入一行,直到读入null为文件结束 131 while ((tempString = reader.readLine()) != null) { 132 // 显示行号 133 buf.append(tempString+"</br>"); 134 } 135 reader.close(); 136 } catch (IOException e) { 137 e.printStackTrace(); 138 } finally { 139 if (reader != null) { 140 try { 141 reader.close(); 142 } catch (IOException e1) { 143 } 144 } 145 } 146 return buf.toString();147 } 148 149 /** 150 * 以字符为单位读取文件,常用于读文本,数字等类型的文件 151 * @param fileName 文件的路径152 * @return153 */ 154 public static String readFileByChars(String fileName) { 155 File file = new File(fileName); 156 Reader reader = null; 157 StringBuffer buf = new StringBuffer();158 try { 159 // 一次读一个字符 160 reader = new InputStreamReader(new FileInputStream(file)); 161 int tempchar; 162 while ((tempchar = reader.read()) != -1) { 163 // 对于windows下,\r\n这两个字符在一起时,表示一个换行。 164 // 但如果这两个字符分开显示时,会换两次行。 165 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。 166 if (((char) tempchar) != '\r') { 167 buf.append((char) tempchar); 168 } 169 } 170 reader.close(); 171 } catch (Exception e) { 172 e.printStackTrace(); 173 } 174 return buf.toString();175 } 176 177 /** 178 * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 179 * MappedByteBuffer 可以在处理大文件时,提升性能 180 * @param fileName 文件的路径181 * @return182 */ 183 public static byte[] readFileByBytes(String fileName) { 184 FileChannel fc = null; 185 byte[] result = null;186 try{ 187 fc = new RandomAccessFile(fileName,"r").getChannel();188 MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size()).load(); 189 result = new byte[(int)fc.size()]; 190 if (byteBuffer.remaining() > 0) { 191 byteBuffer.get(result, 0, byteBuffer.remaining()); 192 } 193 }catch (IOException e) { 194 e.printStackTrace(); 195 }finally{ 196 try{ 197 fc.close(); 198 }catch (IOException e) { 199 e.printStackTrace(); 200 } 201 } 202 return result;203 } 204 205 /**206 * 读取properties配置文件207 * @param filePath 文件路径208 * @return209 */210 public static Properties loadProperty(String filePath) {211 Properties prop=new Properties();212 try {213 File file = new File(filePath);214 prop.load(new FileReader(file.getAbsolutePath()));215 }catch (IOException e) {216 e.printStackTrace();217 }218 return prop;219 } 220 221 /**222 * 转换文件大小223 * @param fileS 文件长度,单位byte224 * @return 单位B、K、M、G225 */226 public static String FormetFileSize(long fileS) {227 DecimalFormat df = new DecimalFormat("#.00");228 String fileSizeString = "";229 if (fileS < 1024) {230 fileSizeString = df.format((double) fileS) + "B";231 } else if (fileS < 1048576) {232 fileSizeString = df.format((double) fileS / 1024) + "K";233 } else if (fileS < 1073741824) {234 fileSizeString = df.format((double) fileS / 1048576) + "M";235 } else {236 fileSizeString = df.format((double) fileS / 1073741824) + "G";237 }238 return fileSizeString;239 }240 241 /**242 * 压缩多个文件成一个zip文件243 * @param srcfile 源文件列表244 * @param zipfile 压缩后的文件245 * @return 压缩是否成功246 */247 public static boolean zip(File[] srcfile, File zipfile) {248 byte[] buf = new byte[1024];249 try {250 // ZipOutputStream类:完成文件或文件夹的压缩251 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));252 for (int i = 0; i < srcfile.length; i++) {253 FileInputStream in = new FileInputStream(srcfile[i]);254 out.putNextEntry(new ZipEntry(srcfile[i].getName()));255 int len;256 while ((len = in.read(buf)) > 0) {257 out.write(buf, 0, len);258 }259 out.closeEntry();260 in.close();261 }262 out.close();263 return true;264 } catch (Exception e) {265 return false;266 }267 }268 269 /**270 * 解压缩zip文件271 * @param zipfile 需要解压缩的文件272 * @param descDir 解压后的目标目录273 * @return 解压是否成功274 */275 public static boolean unZip(File zipfile, String descDir) {276 try {277 ZipFile zf = new ZipFile(zipfile);278 for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {279 ZipEntry entry = (ZipEntry) entries.nextElement();280 String zipEntryName = entry.getName();281 InputStream in = zf.getInputStream(entry);282 OutputStream out = new FileOutputStream(descDir + zipEntryName);283 byte[] buf1 = new byte[1024];284 int len;285 while ((len = in.read(buf1)) > 0) {286 out.write(buf1, 0, len);287 }288 in.close();289 out.close();290 }291 return true;292 } catch (Exception e) {293 return false;294 }295 }296 297 /**298 * 通过jcom后台打印excel文件,需要服务以进程方式启动299 * @param fname 文件路径300 * @return 打印是否成功301 */302 public static boolean printExcel4Jcom(String fname){303 ReleaseManager rm = new ReleaseManager(); 304 try{ 305 ExcelApplication excel = new ExcelApplication(rm); 306 ExcelWorkbooks xlBooks = excel.Workbooks(); 307 ExcelWorkbook xlBook = xlBooks.Open(fname); 308 ExcelWorksheet xlSheet = excel.ActiveSheet(); 309 xlSheet.PrintOut(); 310 xlBook.Close(false, null, false); 311 excel.Quit(); 312 }catch(Exception e){ 313 e.printStackTrace(); 314 return false; 315 }finally{ 316 rm.release(); 317 } 318 return true; 319 }320 321 /**322 * 通过jacob后台打印excel文件,需要服务以进程方式启动323 * @param path Excel文件路径324 * @return 打印是否成功325 */326 public static boolean printExcel4Jacob(String path){327 ComThread.InitSTA();328 ActiveXComponent xl = new ActiveXComponent("Excel.Application");329 try {330 // 不打开文档331 // Dispatch.put(xl, "Visible", new Variant(false));332 Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();333 334 Dispatch workbook = Dispatch.call(workbooks, "Open", path).toDispatch();335 // 开始打印336 // Dispatch.get(workbook, "PrintOut");337 Dispatch.callN(workbook, "PrintOut", new Object[] { Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),338 new Boolean(false), null, new Boolean(true), Variant.VT_MISSING, "" });339 Dispatch.call(workbook, "Close");340 System.out.println("打印执行成功");341 } catch (Exception e) {342 System.out.println("JACOB打印失败"+e);343 return false;344 } finally {345 // 始终释放资源346 xl.invoke("Quit", new Variant[] {});347 ComThread.Release();348 }349 return true;350 }351 352 /**353 * 文件属性类354 */355 public static class FileProperties{356 357 private String id;358 359 /**360 * 文件大小361 */362 private String fileSize;363 364 /**365 * 文件名称366 */367 private String fileName;368 369 370 public String getId() {371 return id;372 }373 public void setId(String id) {374 this.id = id;375 }376 377 public String getFileSize() {378 return fileSize;379 }380 public void setFileSize(String fileSize) {381 this.fileSize = fileSize;382 }383 384 public String getFileName() {385 return fileName;386 }387 public void setFileName(String fileName) {388 this.fileName = fileName;389 }390 }391 }
文章转载自:http://www.cnblogs.com/Mr-kevin/p/5559033.html
0 0
- 常用文件(夹)处理方法工具类
- 常用cookie处理方法工具类
- 文件操作工具类 FileUtils常用方法
- 文件工具类,一些常用的文件方法
- 文件处理工具类
- 文件处理工具类
- 一个常用处理文件与文件夹方法的综合类
- JAVA IO处理文件的常用方法
- 有关file文件处理的常用方法
- 23个常用的文件处理方法
- Android常用文件工具类
- JAVA常用文件工具类
- Java 文件处理工具类
- 日期工具类常用方法
- BigDecimal常用方法工具类
- 工具类Collections常用方法
- 日期工具类处理方法
- Android中常用的bitmap处理方法 (bitmap工具类)
- 自我能力的提升方法
- ios设置导航栏属性
- JNDI 笔记(一) 概述
- C++ STL优先队列常用用法
- LRU Caching
- 常用文件(夹)处理方法工具类
- window.screen
- 初识STL
- 日常练习-xml成绩管练习
- 常用类型转换方法工具类
- 理科十大热门专业
- INRIA数据集
- python中比较数据大小的问题
- 10 款必备的 Sublime Text 插件