用于读取UTF8 cvs和excel文件的java字节顺序标记
朋友
我必须使用BOM(字节顺序标记),以确保下载的文件在cvs和excel中以UTF-8格式正确显示
**
My question is can BOM be applied to FileOutputStream instead of OutputStream as below ?
***
String targetfileName = reportXMLFileName.substring(0, reportXMLFileName.lastIndexOf(".") + 1);
targetfileName += "xlsx";
File tmpFile=new File((filePath !=null?filePath.trim():"")+"/"+templateFile);
FileOutputStream out = new FileOutputStream((filePath !=null?filePath.trim():"")+"/"+targetfileName);
/* Here is the example*/
out.write(new byte[] {(byte)0xEF, (byte)0xBB, (byte)0xBF });
substitute(tmpFile, tmp, sheetRef.substring(1), out);
out.close();
# 1 楼答案
(如评论中所问。)
在下面的
file
中,可能是File、(File)OutputStream或String(filename)或者从Java 7开始:
使用文本可以更自然地使用编写器,因为不需要自己使用
String.getBytes("UTF-8")
转换字符串# 2 楼答案
对。BOM位于任何数据流的开头,无论是通过网络还是文件。只需以相同的方式在开始时将BOM写入文件,只需写入
FileOutputStream
。无论如何,记住FileOutputStream是OutputStream的一种类型