• 文件流字符串互转


    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. }
  • 相关阅读:
    scp(secure copy)安全拷贝
    rsync 远程同步工具
    Ansible:遇到错误 "sudo: /etc/sudoers is world writable sudo: no valid sudoers sources found, quitting
    kafka数据分区的四种策略
    SwitchHosts—hosts管理利器
    HDU 2577 How to Type (字符串处理)
    HDU 1465 不容易系列之一 (错排公式+容斥)
    FZUOJ 2205 据说题目很水 (无三元环图最大边数)
    约瑟夫环问题 ( 最简单的数学解法)
    POJ 3279 Fliptile ( 开关问题)
  • 原文地址:https://www.cnblogs.com/dashazia/p/11477072.html
Copyright © 2020-2023  润新知