Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535439
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-30 14:09:16


点击(此处)折叠或打开


  1. package com.fh.util;

  2. import java.io.BufferedInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.RandomAccessFile;
  9. import java.nio.ByteBuffer;
  10. import java.nio.MappedByteBuffer;
  11. import java.nio.channels.FileChannel;
  12. import java.nio.channels.FileChannel.MapMode;

  13. /**
  14.  * 说明:文件处理
  15.  * 作者:FH Admin
  16.  * from:fhadmin.cn
  17.  */
  18. public class FileUtil {

  19.     
  20.     /**获取文件大小 返回 KB 保留3位小数 没有文件时返回0
  21.      * @param filepath 文件完整路径,包括文件名
  22.      * @return
  23.      */
  24.     public static Double getFilesize(String filepath){
  25.         File backupath = new File(filepath);
  26.         return Double.valueOf(backupath.length())/1000.000;
  27.     }
  28.     
  29.     /**
  30.      * 创建目录
  31.      * @param destDirName目标目录名
  32.      * @return
  33.      */
  34.     public static Boolean createDir(String destDirName) {
  35.         File dir = new File(destDirName);
  36.         if(!dir.getParentFile().exists()){                //判断有没有父路径,就是判断文件整个路径是否存在
  37.             return dir.getParentFile().mkdirs();        //不存在就全部创建
  38.         }
  39.         return false;
  40.     }

  41.     /**
  42.      * 删除文件
  43.      * @param filePathAndName
  44.      * String 文件路径及名称 如c:/fqf.txt
  45.      * @param fileContent
  46.      * String
  47.      * @return boolean
  48.      */
  49.     public static void delFile(String filePathAndName) {
  50.         try {
  51.             String filePath = filePathAndName;
  52.             filePath = filePath.toString();
  53.             java.io.File myDelFile = new java.io.File(filePath);
  54.             myDelFile.delete();
  55.         } catch (Exception e) {
  56.             System.out.println("删除文件操作出错");
  57.             e.printStackTrace();
  58.         }
  59.     }

  60.     /**
  61.      * 读取到字节数组0
  62.      * @param filePath //路径
  63.      * @throws IOException
  64.      */
  65.     public static byte[] getContent(String filePath) throws IOException {
  66.         File file = new File(filePath);
  67.         long fileSize = file.length();
  68.         if (fileSize > Integer.MAX_VALUE) {
  69.             System.out.println("file too big...");
  70.             return null;
  71.         }
  72.         FileInputStream fi = new FileInputStream(file);
  73.         byte[] buffer = new byte[(int) fileSize];
  74.         int offset = 0;
  75.         int numRead = 0;
  76.         while (offset < buffer.length
  77.                 && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
  78.             offset += numRead;
  79.         }
  80.         // 确保所有数据均被读取
  81.         if (offset != buffer.length) {
  82.             throw new IOException("Could not completely read file " + file.getName());
  83.         }
  84.         fi.close();
  85.         return buffer;
  86.     }

  87.     /**
  88.      * 读取到字节数组1
  89.      *
  90.      * @param filePath
  91.      * @return
  92.      * @throws IOException
  93.      */
  94.     public static byte[] toByteArray(String filePath) throws IOException {

  95.         File f = new File(filePath);
  96.         if (!f.exists()) {
  97.             throw new FileNotFoundException(filePath);
  98.         }
  99.         ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
  100.         BufferedInputStream in = null;
  101.         try {
  102.             in = new BufferedInputStream(new FileInputStream(f));
  103.             int buf_size = 1024;
  104.             byte[] buffer = new byte[buf_size];
  105.             int len = 0;
  106.             while (-1 != (len = in.read(buffer, 0, buf_size))) {
  107.                 bos.write(buffer, 0, len);
  108.             }
  109.             return bos.toByteArray();
  110.         } catch (IOException e) {
  111.             e.printStackTrace();
  112.             throw e;
  113.         } finally {
  114.             try {
  115.                 in.close();
  116.             } catch (IOException e) {
  117.                 e.printStackTrace();
  118.             }
  119.             bos.close();
  120.         }
  121.     }

  122.     /**
  123.      * 读取到字节数组2
  124.      *
  125.      * @param filePath
  126.      * @return
  127.      * @throws IOException
  128.      */
  129.     public static byte[] toByteArray2(String filePath) throws IOException {
  130.         File f = new File(filePath);
  131.         if (!f.exists()) {
  132.             throw new FileNotFoundException(filePath);
  133.         }
  134.         FileChannel channel = null;
  135.         FileInputStream fs = null;
  136.         try {
  137.             fs = new FileInputStream(f);
  138.             channel = fs.getChannel();
  139.             ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
  140.             while ((channel.read(byteBuffer)) > 0) {
  141.                 // do nothing
  142.                 // System.out.println("reading");
  143.             }
  144.             return byteBuffer.array();
  145.         } catch (IOException e) {
  146.             e.printStackTrace();
  147.             throw e;
  148.         } finally {
  149.             try {
  150.                 channel.close();
  151.             } catch (IOException e) {
  152.                 e.printStackTrace();
  153.             }
  154.             try {
  155.                 fs.close();
  156.             } catch (IOException e) {
  157.                 e.printStackTrace();
  158.             }
  159.         }
  160.     }

  161.     /**
  162.      * Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
  163.      *
  164.      * @param filename
  165.      * @return
  166.      * @throws IOException
  167.      */
  168.     public static byte[] toByteArray3(String filePath) throws IOException {

  169.         FileChannel fc = null;
  170.         RandomAccessFile rf = null;
  171.         try {
  172.             rf = new RandomAccessFile(filePath, "r");
  173.             fc = rf.getChannel();
  174.             MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
  175.                     fc.size()).load();
  176.             //System.out.println(byteBuffer.isLoaded());
  177.             byte[] result = new byte[(int) fc.size()];
  178.             if (byteBuffer.remaining() > 0) {
  179.                 // System.out.println("remain");
  180.                 byteBuffer.get(result, 0, byteBuffer.remaining());
  181.             }
  182.             return result;
  183.         } catch (IOException e) {
  184.             e.printStackTrace();
  185.             throw e;
  186.         } finally {
  187.             try {
  188.                 rf.close();
  189.                 fc.close();
  190.             } catch (IOException e) {
  191.                 e.printStackTrace();
  192.             }
  193.         }
  194.     }

  195. }


阅读(793) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~