工作笔记之—文件编码转换
2016-03-23 15:20
399 查看
今天老大给了我一个任务,我之前写的文件管理系统中有一个读txt文件的功能。但是这个只支持utf-8的编码格式。新的需求是让它支持更多的编码格式,不然读出来可就是乱码喽。
下面说一下具体的解决方法。
1.判断txt文件的编码格式
相信很多人知道,txt文件的编码格式一共有四种,分别是“GBK”,“UTF-8”,“Unicode”,‘“UTF-16BE”。这些编码格式的区别在于写入头文件的信息不同,为了在读文件的时候不出现乱码,我们需要判断出你所要读的文件的编码类型,然后以这种类型来读文件。
第一步,判断文件的编码类型:
public String codeString(File file) {
BufferedInputStream bin;
int p = 0;
try {
bin = new BufferedInputStream(new FileInputStream(file));
p = (bin.read() << 8) + bin.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
2.以文件自己的编码类型读文件本身
将上面方法的返回值作为参数传进读文件这个方法。
public String readTxtFile(File file, String code) {
StringBuffer txtFile = new StringBuffer();
try {
InputStream inputStream = new FileInputStream(file);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, code);
BufferedReader buffreader = new BufferedReader(
inputStreamReader);
String line;
// 分行读取
while ((line = buffreader.readLine()) != null) {
txtFile.append(line + "\n");
}
inputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return txtFile.toString();
}
下面说一下具体的解决方法。
1.判断txt文件的编码格式
相信很多人知道,txt文件的编码格式一共有四种,分别是“GBK”,“UTF-8”,“Unicode”,‘“UTF-16BE”。这些编码格式的区别在于写入头文件的信息不同,为了在读文件的时候不出现乱码,我们需要判断出你所要读的文件的编码类型,然后以这种类型来读文件。
第一步,判断文件的编码类型:
public String codeString(File file) {
BufferedInputStream bin;
int p = 0;
try {
bin = new BufferedInputStream(new FileInputStream(file));
p = (bin.read() << 8) + bin.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String code = null;
switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}
2.以文件自己的编码类型读文件本身
将上面方法的返回值作为参数传进读文件这个方法。
public String readTxtFile(File file, String code) {
StringBuffer txtFile = new StringBuffer();
try {
InputStream inputStream = new FileInputStream(file);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, code);
BufferedReader buffreader = new BufferedReader(
inputStreamReader);
String line;
// 分行读取
while ((line = buffreader.readLine()) != null) {
txtFile.append(line + "\n");
}
inputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return txtFile.toString();
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories