Android中读取中文字符的文件与文件读取相关(二)

2014-11-24 03:20:02 · 作者: · 浏览: 7
read(bytes);


str = EncodingUtils.getString(bytes,"UTF-8");


fin.close();


}


catch(Exception e){


e.printStackTrace();


}


return str;


}


(4) sdcard目录下的文件存取(/mnt/sdcard/)


使用FileOutputStream写文件:


public void writeFile2Sdcard(String fileName,String message){


try{


FileOutputStream fout = new FileOutputStream(fileName);


byte [] bytes =message.getBytes();


fout.write(bytes);


fout.close();


}


catch(Exception e){


e.printStackTrace();


}


}


使用FileInputStream读文件:


public String readFileFromSdcard(String fileName){


String res="";


try{


FileInputStream fin = newFileInputStream(fileName);


int length =fin.available();


byte [] buffer = newbyte[length];


fin.read(buffer);


res =EncodingUtils.getString(buffer, "UTF-8");


fin.close();


}


catch(Exception e){


e.printStackTrace();


}


return res;


}