zoukankan      html  css  js  c++  java
  • 文件流字符串互转

    1. import java.io.ByteArrayInputStream;
    2. import java.io.ByteArrayOutputStream;
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.FileReader;
    7. import java.io.FileWriter;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10.  
    11. import sun.misc.BASE64Decoder;
    12.  
    13. public class FileStrUtil {
    14. /**
    15. * summary:将字符串存储为文件 采用Base64解码
    16. *
    17. * @param fileStr
    18. * @param outfile
    19. *
    20. */
    21. public static void streamSaveAsFile(InputStream is, String outFileStr) {
    22. FileOutputStream fos = null;
    23. try {
    24. File file = new File(outFileStr);
    25. BASE64Decoder decoder = new BASE64Decoder();
    26. fos = new FileOutputStream(file);
    27. byte[] buffer = decoder.decodeBuffer(is);
    28. fos.write(buffer, 0, buffer.length);
    29. } catch (Exception e) {
    30. e.printStackTrace();
    31. throw new RuntimeException(e);
    32. } finally {
    33. try {
    34. is.close();
    35. fos.close();
    36. } catch (Exception e2) {
    37. e2.printStackTrace();
    38. throw new RuntimeException(e2);
    39. }
    40. }
    41. }
    42.  
    43. /**
    44. *
    45. *
    46. * summary:将字符串存储为文件
    47. *
    48. * @param fileStr
    49. * @param outfile
    50. *
    51. */
    52. public static void stringSaveAsFile(String fileStr, String outFilePath) {
    53. InputStream out = new ByteArrayInputStream(fileStr.getBytes());
    54. FileStrUtil.streamSaveAsFile(out, outFilePath);
    55. }
    56.  
    57. /**
    58. * 将流转换成字符串 使用Base64加密
    59. *
    60. * @param in输入流
    61. * @return
    62. * @throws IOException
    63. */
    64. public static String streamToString(InputStream inputStream) throws IOException {
    65. byte[] bt = toByteArray(inputStream);
    66. inputStream.close();
    67. String out = new sun.misc.BASE64Encoder().encodeBuffer(bt);
    68. return out;
    69. }
    70.  
    71. /**
    72. * 将流转换成字符串
    73. *
    74. * @param in输入流
    75. * @return
    76. * @throws IOException
    77. */
    78. public static String fileToString(String filePath) throws IOException {
    79. File file = new File(filePath);
    80. FileInputStream is = new FileInputStream(file);
    81. String fileStr = FileStrUtil.streamToString(is);
    82. return fileStr;
    83. }
    84.  
    85. /**
    86. *
    87. * summary:将流转化为字节数组
    88. *
    89. * @param inputStream
    90. * @return
    91. * @throws IOException
    92. *
    93. */
    94. public static byte[] toByteArray(InputStream inputStream) throws IOException {
    95. ByteArrayOutputStream out = new ByteArrayOutputStream();
    96. byte[] buffer = new byte[1024 * 4];
    97. byte[] result = null;
    98. try {
    99. int n = 0;
    100. while ((n = inputStream.read(buffer)) != -1) {
    101. out.write(buffer, 0, n);
    102. }
    103. result = out.toByteArray();
    104. } catch (Exception e) {
    105. e.printStackTrace();
    106. throw new RuntimeException(e);
    107. } finally {
    108. out.close();
    109. }
    110. return result;
    111. }
    112.  
    113. public static void main(String[] args) throws Exception {
    114. String fromPath = "F://fileupload//aaa.docx";
    115. String toPath = "C://Users//Desktop//aaaa.docx";
    116. String fileStr = FileStrUtil.fileToString(fromPath);
    117. FileStrUtil.stringSaveAsFile(fileStr, toPath);
    118. }
    119. }
  • 相关阅读:
    python 的rjust函数
    二叉树
    实验四 系统调用
    实验三:跟踪分析Linux内核的启动过程
    ZigZag Conversion1
    Oracle数据文件管理
    Java中hashcode,equals和==
    浅析Java中HashMap的实现
    迷宫(栈,堆,队列)
    TCP/IP的三次握手协议
  • 原文地址:https://www.cnblogs.com/dashazia/p/11477072.html
Copyright © 2011-2022 走看看