java系统间文件流传输,解决乱编问题

该文介绍了如何在A系统中将GBK编码的文件转换为UTF-8编码并复制到新文件,然后提供以UTF-8格式的对外接口。B系统接收转换后的数据,使用InputStream和IOUtils读取并处理为UTF-8编码的字符串列表。
摘要由CSDN通过智能技术生成

目标:

完成不同系统间文件流传输,解决乱编问题


内容:

A系统:

1.完成文件复制,编码规范(GBK -> UTF-8)

InputStreamReader inputStream = null;
OutputStreamWriter outputStream = null;
//oldFile\newFile为new File()对象
inputStream = new InputStreamReader(new FileInputStream(oldFile),"GBK");
outputStream = new OutputStreamWriter(new FileOutputStream(newFile),"UTF-8");

char[] buf = new char[1024];
int bytesRead;
while((bytesRead = inputStream.read(buf))>0){
    outputStream.write(buf,0,bytesRead);
}

2.对外接口,以 UTF-8 格式输出

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File(newFile)));

byte[] buf = new byte[1024];
int bytes;
while((bytes=inputStream.read(buf))>0){
    outputStream.write(buf,0,bytes);
} 

String s = new String(outputStream.toByteArray(),"UTF-8");

B系统:

1.接送 

InputStream is = new ByteArrayInputStream(s.getBytes());
List<String> readLines = IOUtils.readLines(is,"UTF-8");

结论:

此文章给出了重点代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值