Java讀取、寫入文件若何處理亂碼成績。本站提示廣大學習愛好者:(Java讀取、寫入文件若何處理亂碼成績)文章只能為提供參考,不一定能成為您想要的結果。以下是Java讀取、寫入文件若何處理亂碼成績正文
讀取文件流時,常常會碰到亂碼的景象,形成亂碼的緣由固然弗成能是一個,這裡重要引見由於文件編碼格局而招致的亂碼的成績。起首,明白一點,文本文件與二進制文件的概念與差別。
文本文件是基於字符編碼的文件,罕見的編碼有ASCII編碼,UNICODE編碼、ANSI編碼等等。二進制文件是基於值編碼的文件,你可以依據詳細運用,指定某個值是甚麼意思(如許一個進程,可以看做是自界說編碼。)
是以可以看出文本文件根本上是定長編碼的(也有非定長的編碼如UTF-8)。而二進制文件可算作是變長編碼的,由於是值編碼嘛,若干個比特代表一個值,完整由你決議。
關於二進制文件,是萬萬不克不及應用字符串的,由於字符串默許初始化時會應用體系默許編碼,但是,二進制文件由於自界說編碼天然與固定格局的編碼會有所抵觸,所以關於二進制的文件只能采取字撙節讀取、操作、寫入。
關於文本文件,由於編碼固定,所以只需在讀取文件之前,采取文件本身的編碼格局解析文件,然後獲得字節,再然後,經由過程指定格局初始化字符串,那末獲得的文本是不會亂碼的。固然,二進制文件也能夠獲得到它的文本編碼格局,然則那是禁絕確的,所以不克不及同日而語。
詳細操作以下:
1)獲得文本文件的格局
public static String getFileEncode(String path) {
String charset ="asci";
byte[] first3Bytes = new byte[3];
BufferedInputStream bis = null;
try {
boolean checked = false;
bis = new BufferedInputStream(new FileInputStream(path));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "Unicode";//UTF-16LE
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
charset = "Unicode";//UTF-16BE
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) {
charset = "UTF8";
checked = true;
}
bis.reset();
if (!checked) {
int len = 0;
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) //零丁湧現BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF)
//雙字節 (0xC0 - 0xDF) (0x80 - 0xBF),也能夠在GB編碼內
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) { //也有能夠失足,然則概率較小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
//TextLogger.getLogger().info(loc + " " + Integer.toHexString(read));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException ex) {
}
}
}
return charset;
}
private static String getEncode(int flag1, int flag2, int flag3) {
String encode="";
// txt文件的開首會多出幾個字節,分離是FF、FE(Unicode),
// FE、FF(Unicode big endian),EF、BB、BF(UTF-8)
if (flag1 == 255 && flag2 == 254) {
encode="Unicode";
}
else if (flag1 == 254 && flag2 == 255) {
encode="UTF-16";
}
else if (flag1 == 239 && flag2 == 187 && flag3 == 191) {
encode="UTF8";
}
else {
encode="asci";// ASCII碼
}
return encode;
}
2)經由過程文件的編碼格局讀取文件流
/**
* 經由過程途徑獲得文件的內容,這個辦法由於用到了字符串作為載體,為了准確讀取文件(穩定碼),只能讀取文本文件,平安辦法!
*/
public static String readFile(String path){
String data = null;
// 斷定文件能否存在
File file = new File(path);
if(!file.exists()){
return data;
}
// 獲得文件編碼格局
String code = FileEncode.getFileEncode(path);
InputStreamReader isr = null;
try{
// 依據編碼格局解析文件
if("asci".equals(code)){
// 這裡采取GBK編碼,而不消情況編碼格局,由於情況默許編碼不等於操作體系編碼
// code = System.getProperty("file.encoding");
code = "GBK";
}
isr = new InputStreamReader(new FileInputStream(file),code);
// 讀取文件內容
int length = -1 ;
char[] buffer = new char[1024];
StringBuffer sb = new StringBuffer();
while((length = isr.read(buffer, 0, 1024) ) != -1){
sb.append(buffer,0,length);
}
data = new String(sb);
}catch(Exception e){
e.printStackTrace();
log.info("getFile IO Exception:"+e.getMessage());
}finally{
try {
if(isr != null){
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
log.info("getFile IO Exception:"+e.getMessage());
}
}
return data;
}
3)經由過程文件指定的格局寫入文件
/**
* 依照指定的途徑和編碼格局保留文件內容,這個辦法由於用到了字符串作為載體,為了准確寫入文件(穩定碼),只能寫入文本內容,平安辦法
*
* @param data
* 將要寫入到文件中的字節數據
* @param path
* 文件途徑,包括文件名
* @return boolean
* 當寫入終了時前往true;
*/
public static boolean writeFile(byte data[], String path , String code){
boolean flag = true;
OutputStreamWriter osw = null;
try{
File file = new File(path);
if(!file.exists()){
file = new File(file.getParent());
if(!file.exists()){
file.mkdirs();
}
}
if("asci".equals(code)){
code = "GBK";
}
osw = new OutputStreamWriter(new FileOutputStream(path),code);
osw.write(new String(data,code));
osw.flush();
}catch(Exception e){
e.printStackTrace();
log.info("toFile IO Exception:"+e.getMessage());
flag = false;
}finally{
try{
if(osw != null){
osw.close();
}
}catch(IOException e){
e.printStackTrace();
log.info("toFile IO Exception:"+e.getMessage());
flag = false;
}
}
return flag;
}
4)關於二進制文件並且內容很少的,例如Word文檔等,可使用以下方法讀取、寫入文件
/**
* 從指定途徑讀取文件到字節數組中,關於一些非文本格局的內容可以選用這個辦法
* 457364578634785634534
* @param path
* 文件途徑,包括文件名
* @return byte[]
* 文件字節數組
*
*/
public static byte[] getFile(String path) throws IOException {
FileInputStream stream=new FileInputStream(path);
int size=stream.available();
byte data[]=new byte[size];
stream.read(data);
stream.close();
stream=null;
return data;
}
/**
* 把字節內容寫入到對應的文件,關於一些非文本的文件可以采取這個辦法。
* @param data
* 將要寫入到文件中的字節數據
* @param path
* 文件途徑,包括文件名
* @return boolean isOK 當寫入終了時前往true;
* @throws Exception
*/
public static boolean toFile(byte data[], String path) throws Exception {
FileOutputStream out=new FileOutputStream(path);
out.write(data);
out.flush();
out.close();
out=null;
return true;
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。