NettyFileUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cn.nosum.common.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.nio.ByteBuffer;
  8. import java.nio.channels.FileChannel;
  9. public class NettyFileUtil {
  10. private static final Logger logger = LoggerFactory.getLogger(NettyFileUtil.class);
  11. private final static Integer BYTE_BUFFER_LENGTH=1024;
  12. private final static String LINE_SEPARATOR= System.getProperty("line.separator");
  13. /**
  14. * 将内存中的数据写入到磁盘,如果路径不存在则创建
  15. * @param data 文件内容
  16. * @param filePath 文件保存路劲
  17. * @param append 文件存在时是否追加内容
  18. */
  19. public static void dataToFile(String data, String filePath,boolean append) {
  20. File file = new File(filePath);
  21. if (!file.getParentFile().exists()) {
  22. file.getParentFile().mkdirs();
  23. }
  24. try (FileOutputStream fileOutputStream = new FileOutputStream(file,append);
  25. FileChannel fileChannel = fileOutputStream.getChannel()) {
  26. int i = 0;
  27. byte[] writeDate=data.getBytes();
  28. int length = writeDate.length;
  29. ByteBuffer byteBuffer = ByteBuffer.allocate(getLength(length));
  30. // 一次性读取完毕
  31. if (BYTE_BUFFER_LENGTH > length) {
  32. byteBuffer.put(writeDate, i, length);
  33. byteBuffer.flip();
  34. fileChannel.write(byteBuffer);
  35. } else {
  36. // 循环读取
  37. for (int temp = 0; temp < length; temp += BYTE_BUFFER_LENGTH) {
  38. byteBuffer.clear();
  39. byteBuffer.put(writeDate, temp, BYTE_BUFFER_LENGTH);
  40. byteBuffer.flip();
  41. fileChannel.write(byteBuffer);
  42. }
  43. }
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. /**
  49. * 读取文件内容
  50. * @param filePath 目标文件路径
  51. * @return 文件内容
  52. */
  53. public static String dataFromFile(String filePath) {
  54. File file = new File(filePath);
  55. // 从输入流中获取 channel
  56. try (FileInputStream fileInputStream = new FileInputStream(file);
  57. FileChannel channel = fileInputStream.getChannel()) {
  58. // 分配缓冲区
  59. ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_LENGTH);
  60. StringBuilder result = new StringBuilder();
  61. while (true) {
  62. byteBuffer.clear();
  63. // 将 channel数据写到buffer中
  64. int read = channel.read(byteBuffer);
  65. // 因为byteBuffer大小原因,因此需要用一个中间字符串接受一下
  66. result.append(new String(byteBuffer.array()));
  67. if (read == -1) {
  68. break;
  69. }
  70. }
  71. return result.toString();
  72. } catch (Exception e) {
  73. logger.error("文件读取错误,错误原因 :{0}", e);
  74. }
  75. return null;
  76. }
  77. /**
  78. * 文件复制
  79. * @param sourceFilePath 源路径
  80. * @param targetFilePath 目标路劲
  81. */
  82. public static void copyFileUseChannelTransfer(String sourceFilePath, String targetFilePath) {
  83. File source = new File(sourceFilePath);
  84. File target = new File(targetFilePath);
  85. try (FileInputStream fileInputStream = new FileInputStream(source);
  86. FileOutputStream fileOutputStream = new FileOutputStream(target);
  87. FileChannel fileInputStreamChannel = fileInputStream.getChannel();
  88. FileChannel fileOutputStreamChannel = fileOutputStream.getChannel()) {
  89. // 直接将输入channel复制到输出channel
  90. fileOutputStreamChannel.transferFrom(
  91. fileInputStreamChannel,
  92. fileInputStreamChannel.position(),
  93. fileInputStreamChannel.size()
  94. );
  95. } catch (Exception e) {
  96. logger.error("文件复制错误,错误原因 :{0}", e);
  97. }
  98. }
  99. public static int getLength(Integer length){
  100. return BYTE_BUFFER_LENGTH>length?length:BYTE_BUFFER_LENGTH;
  101. }
  102. }