最近項目中遇到一個問題,客戶電腦中的自造字在JAVA中認不出來,導致上傳的文件亂碼顯示。
此方法雖然不能正確顯示出自造字,可是可以查找到亂碼的部分。(哪位大神如果知道JAVA中顯示系統自造字請告知呵!)
FileInputStream fis = new FileInputStream(filePath);
int b = fis.read();
List res = new ArrayList();
ByteOutputStream bos = new ByteOutputStream();
while (b != -1) {
if (b != '\r' && b != '\n') {
bos.write(b);
}
if (b == '\n') { // suppose there always are "\r\n"
res.add(Arrays.copyOf(bos.getBytes(), bos.getCount()));
bos.close();
bos = new ByteOutputStream();
}
b = fis.read();
}
if (bos.getBytes().length > 0) {
res.add(Arrays.copyOf(bos.getBytes(), bos.getCount()));
bos.close();
}
fis.close();
int b = fis.read();
List res = new ArrayList();
ByteOutputStream bos = new ByteOutputStream();
while (b != -1) {
if (b != '\r' && b != '\n') {
bos.write(b);
}
if (b == '\n') { // suppose there always are "\r\n"
res.add(Arrays.copyOf(bos.getBytes(), bos.getCount()));
bos.close();
bos = new ByteOutputStream();
}
b = fis.read();
}
if (bos.getBytes().length > 0) {
res.add(Arrays.copyOf(bos.getBytes(), bos.getCount()));
bos.close();
}
fis.close();
File f = new File(fileName);
if (!f.exists()) {
File dir = new File(f.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
}
FileOutputStream fos = new FileOutputStream(f,true);
byte[] bytes = new String(content, "big5").getBytes("big5");
if (bytes.length != content.length) {
for (int j = 0, k = 0; j < content.length; j++) {
if (content[j] != bytes[k++]) {
content[j++] = 63;
if(j content[j] = 63;
}
}
}
}
fos.write(content);
if(islast=="N"){
fos.write('\r');
fos.write('\n');
}
fos.close();
if (!f.exists()) {
File dir = new File(f.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
}
FileOutputStream fos = new FileOutputStream(f,true);
byte[] bytes = new String(content, "big5").getBytes("big5");
if (bytes.length != content.length) {
for (int j = 0, k = 0; j < content.length; j++) {
if (content[j] != bytes[k++]) {
content[j++] = 63;
if(j content[j] = 63;
}
}
}
}
fos.write(content);
if(islast=="N"){
fos.write('\r');
fos.write('\n');
}
fos.close();
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27147629/viewspace-736069/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27147629/viewspace-736069/