目标:
完成不同系统间文件流传输,解决乱编问题
内容:
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");
结论:
此文章给出了重点代码。