java常用类——java常用文件处理方法

Bertha 。 2023-10-17 18:24 169阅读 0赞

开发过程中用到过的一些文件处理方法,整理了一下,方便以后查找参阅。

1.获取指定文件夹下面文件名称集合

  1. /**
  2. * 获取指定路径下所有文件名称集合
  3. */
  4. public static List<String> getFileList(String strPath) {
  5. List<String> filelist=new ArrayList<String>();
  6. File dir = new File(strPath);
  7. File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
  8. if (files != null) {
  9. for (int i = 0; i < files.length; i++) {
  10. String fileName = files[i].getName();
  11. if (files[i].isDirectory()) { // 判断是文件还是文件夹
  12. getFileList(files[i].getAbsolutePath()); // 获取文件绝对路径
  13. } else if (fileName.endsWith("png")||fileName.endsWith("jpg")||fileName.endsWith("JPG")) { // 判断文件名是否以.avi结尾
  14. filelist.add(files[i].getName());
  15. } else {
  16. continue;
  17. }
  18. }
  19. }
  20. return filelist;
  21. }

2.创建文件

  1. /**
  2. * 创建文件
  3. */
  4. public static File createFile(String allPath) throws IOException {
  5. File file = new File(allPath);
  6. if (!file.getParentFile().exists()) {
  7. file.getParentFile().mkdirs();
  8. }
  9. if (!file.exists()) {
  10. file.createNewFile();
  11. }
  12. return file;
  13. }

3.复制文件

  1. /**
  2. * 复制文件
  3. * @param fromFile
  4. * @param toFile
  5. * 2016年12月19日 下午3:31:50
  6. * @throws IOException
  7. */
  8. public static void copyFile(File fromFile,File toFile){
  9. FileInputStream ins=null;
  10. FileOutputStream out=null;
  11. try {
  12. ins = new FileInputStream(fromFile);
  13. out = new FileOutputStream(toFile);
  14. byte[] b = new byte[1024];
  15. int n=0;
  16. while((n=ins.read(b))!=-1){
  17. out.write(b, 0, n);
  18. }
  19. } catch (IOException e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. }
  23. finally{
  24. try {
  25. if(ins!=null)ins.close();
  26. if(ins!=null)out.close();
  27. } catch (IOException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. }
  31. }
  32. }

4.文件压缩

  1. /**
  2. * 文件压缩
  3. * @param srcFile 目录或者单个文件
  4. * @param zipFile 压缩后的ZIP文件
  5. */
  6. public static void doCompress(File srcFile, File zipFile) throws IOException {
  7. ZipOutputStream out = null;
  8. try {
  9. out = new ZipOutputStream(new FileOutputStream(zipFile));
  10. doCompress(srcFile, out);
  11. } catch (Exception e) {
  12. try {
  13. throw e;
  14. } catch (Exception e1) {
  15. // TODO Auto-generated catch block
  16. e1.printStackTrace();
  17. }
  18. } finally {
  19. out.close();//记得关闭资源
  20. }
  21. }
  22. public static void doCompress(String filelName, ZipOutputStream out) throws IOException{
  23. doCompress(new File(filelName), out);
  24. }
  25. public static void doCompress(File file, ZipOutputStream out) throws IOException{
  26. doCompress(file, out, "");
  27. }
  28. public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
  29. if ( inFile.isDirectory() ) {
  30. File[] files = inFile.listFiles();
  31. if (files!=null && files.length>0) {
  32. for (File file : files) {
  33. String name = inFile.getName();
  34. if (!"".equals(dir)) {
  35. name = dir + "/" + name;
  36. }
  37. ZipUtils.doCompress(file, out, name);
  38. }
  39. }
  40. } else {
  41. ZipUtils.doZip(inFile, out, dir);
  42. }
  43. }
  44. public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
  45. String entryName = null;
  46. if (!"".equals(dir)) {
  47. entryName = dir + "/" + inFile.getName();
  48. } else {
  49. entryName = inFile.getName();
  50. }
  51. ZipEntry entry = new ZipEntry(entryName);
  52. out.putNextEntry(entry);
  53. int len = 0 ;
  54. byte[] buffer = new byte[1024];
  55. FileInputStream fis = new FileInputStream(inFile);
  56. while ((len = fis.read(buffer)) > 0) {
  57. out.write(buffer, 0, len);
  58. out.flush();
  59. }
  60. //out.closeEntry();
  61. fis.close();
  62. }

发表评论

表情:
评论列表 (有 0 条评论,169人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java

    java堆栈基本知识: 基本数据类型、局部变量都是存放在栈内存中的,用完就消失。 new创建的实例化对象及数组,是存放在堆内存中的,用完之后靠垃圾回收机制不定期自动消