java 将文件流转化成字符串传输

本文介绍了一种在Java中操作文件的实用方法,包括将字符串存储为文件、将文件转换为字符串、以及使用Base64进行编码和解码。通过这些方法,可以实现文件的高效读写和数据的加密传输。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
 
import sun.misc.BASE64Decoder;
 
public class FileStrUtil {
	/**
	 * summary:将字符串存储为文件 采用Base64解码
	 * 
	 * @param fileStr
	 * @param outfile
	 * 
	 */
	public static void streamSaveAsFile(InputStream is, String outFileStr) {
		FileOutputStream fos = null;
		try {
			File file = new File(outFileStr);
			BASE64Decoder decoder = new BASE64Decoder();
			fos = new FileOutputStream(file);
			byte[] buffer = decoder.decodeBuffer(is);
			fos.write(buffer, 0, buffer.length);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
				fos.close();
			} catch (Exception e2) {
				e2.printStackTrace();
				throw new RuntimeException(e2);
			}
		}
	}
 
	/**
	 * 
	 * 
	 * summary:将字符串存储为文件
	 * 
	 * @param fileStr
	 * @param outfile
	 * 
	 */
	public static void stringSaveAsFile(String fileStr, String outFilePath) {
		InputStream out = new ByteArrayInputStream(fileStr.getBytes());
		FileStrUtil.streamSaveAsFile(out, outFilePath);
	}
 
	/**
	 * 将流转换成字符串 使用Base64加密
	 * 
	 * @param in输入流
	 * @return
	 * @throws IOException
	 */
	public static String streamToString(InputStream inputStream) throws IOException {
		byte[] bt = toByteArray(inputStream);
		inputStream.close();
		String out = new sun.misc.BASE64Encoder().encodeBuffer(bt);
		return out;
	}
 
	/**
	 * 将流转换成字符串
	 * 
	 * @param in输入流
	 * @return
	 * @throws IOException
	 */
	public static String fileToString(String filePath) throws IOException {
		File file = new File(filePath);
		FileInputStream is = new FileInputStream(file);
		String fileStr = FileStrUtil.streamToString(is);
		return fileStr;
	}
 
	/**
	 * 
	 * summary:将流转化为字节数组
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 * 
	 */
	public static byte[] toByteArray(InputStream inputStream) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024 * 4];
		byte[] result = null;
		try {
			int n = 0;
			while ((n = inputStream.read(buffer)) != -1) {
				out.write(buffer, 0, n);
			}
			result = out.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			out.close();
		}
		return result;
	}
 
	public static void main(String[] args) throws Exception {
		String fromPath = "F://fileupload//aaa.docx";
		String toPath = "C://Users//Desktop//aaaa.docx";
		String fileStr = FileStrUtil.fileToString(fromPath);
		FileStrUtil.stringSaveAsFile(fileStr, toPath);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值