Java字符串与文件的互转

1、前言

Java字符串与文件的互转,如果不加密字符串转文件会导致 文件损坏。

2、文件转换为字符串

Base64 是JDK1.8 自带的
在这里插入图片描述

import java.util.Base64;
import java.io.*;
   public static String fileToString(String filePath){
        InputStream  input=null;
        try {
            File file = new File(filePath);
            input= new FileInputStream(file);
            byte[] buffer = toByteArray(input);
            // 读到buffer字节数组中
            input.read(buffer);

            String fileContent=  Base64.getEncoder().encodeToString(buffer);

            return fileContent;

        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

3、文件转换为字符串


    public static boolean stringToFile(String fileContent, String filePath) {
        boolean flag = true;
        byte[] bytes = Base64.getDecoder().decode(fileContent);

        //测试数据的准确性
        InputStream input=null;
        OutputStream output=null;
        try {
            File file = new File(filePath).getParentFile();
            if(!file.exists()){
                file.mkdirs();
            }
            input = new ByteArrayInputStream(bytes);
            output=new FileOutputStream(filePath);
            byte[] buf =new byte[1024*8];
            int n;
            while((n=input.read(buf))!=-1){
                output.write(buf, 0,n);
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            return flag;
        }finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (output != null)
                    output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4、测试

    public static void main(String[] args) {

        String filePath = "C:\\Users\\Administrator\\Desktop\\测试.docx";

        String fileContent=fileToString(filePath);

        String  outFilePath = "F:\\copy\\"+System.currentTimeMillis()+ File.separator +"测试.docx";
        Boolean is= stringToFile(fileContent,outFilePath);

    }

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值