将文本中的内容转换成base64字符串(并不是文本本身,是里面的内容)

    xiaoxiao2022-07-03  134

    package com.xtzn.control; import org.apache.commons.codec.binary.Base64; import java.io.BufferedReader; import java.io.FileReader; import java.nio.charset.StandardCharsets; /** * Create by PSY * * @date: 2019-05-21 16:28 * @version:4.0 * @Description: */ public class Base64ForTxt { public static String encodeBase64File() throws Exception { StringBuffer buffer = new StringBuffer(); String xml =null; try { BufferedReader bf = new BufferedReader(new FileReader("/Users/pengshuyuan/Desktop/Swagger.lua")); String s ; while ((s = bf.readLine()) != null) {//使用readLine方法,一次读一行 buffer.append(s).append("\n"); } xml = buffer.toString(); }catch (Exception e){ } return xml; } public static void main(String[] args) throws Exception { String s = encodeBase64File(); System.out.println(s); byte[] encodeBase64 = Base64.encodeBase64(s.getBytes(StandardCharsets.UTF_8)); System.out.println("RESULT: " + new String(encodeBase64)); } }

    最新回复(0)