android系统中*.bin文件读取时乱码问题
后来我想通过android程序来读取查看不知是什么原因看到的也全部是些乱码,我的查看方法如下:
从android系统中复制出*.bin文件放到我项目工程中assets文件中,读取代码如下:
private AssetManager am;
private InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
readerBin();
}
private void readerBin() {
am = this.getResources().getAssets();
try {
is = am.open("xtra.bin");
if (is != null) {
Log.e("TAG", "It worked!");
}
// *********************方法一***********************
// BufferedReader br = new BufferedReader(new
// InputStreamReader(is,"utf-8"));
//
// String row="",txtStr="";
// while ((row = br.readLine()) != null) {
// txtStr = txtStr + row;
// }
// Log.d("TAG", txtStr);
// ********************方法二************************
// String str=readTextFile(is);
// Log.e("TAG", str);
// ********************方法三************************
String str=convertCodeAndGetText(is);
Log.e("TAG", str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String convertCodeAndGetText(InputStream is) {// 转码
// File file = new File(str_filepath);
BufferedReader reader;
String text = "";
try {
// FileReader f_reader = new FileReader(file);
// BufferedReader reader = new BufferedReader(f_reader);
// FileInputStream fis = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(is);
in.mark(4);
byte[] first3bytes = new byte[3];
in.read(first3bytes);// 找到文档的前三个字节并自动判断文档类型。
in.reset();
if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB&& first3bytes[2] == (byte) 0xBF) {// utf-8
reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
Log.i("TAG", "utf-8");
} else if (first3bytes[0] == (byte) 0xFF
&& first3bytes[1] == (byte) 0xFE) {
reader = new BufferedReader(new InputStreamReader(in, "unicode"));
Log.i("TAG", "unicode");
} else if (first3bytes[0] == (byte) 0xFE&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,"utf-16be"));
Log.i("TAG", "utf-16be");
} else if (first3bytes[0] == (byte) 0xFF&& first3bytes[1] == (byte) 0xFF) {
reader = new BufferedReader(new InputStreamReader(in,"utf-16le"));
Log.i("TAG", "utf-16le");
} else {
reader = new BufferedReader(new InputStreamReader(in, "GBK"));
Log.i("TAG", "GBK");
}
String str = reader.readLine();
while (str != null) {
text = text + str + "\n";
str = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
private String readTextFile(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
String text = new String(buf, "UTF-8");
Log.i("TAG", text);
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
return outputStream.toString();
}
最后,请各位给个处理方法,如果有Demo代码就理好了,先谢谢了。
相关推荐
Android Studio控制台出现中文乱码(方框)问题解决办法
主要介绍了Android Studio控制台出现中文乱码(方框)问题解决办法的相关资料,需要的朋友可以参考下
Android eCryptfs移植实战:内核加密文件系统在移动设备的数据保护应用
文件系统加密是数据安全领域的核心技术,通过在存储层面对文件内容进行透明加密,确保数据在静态存储时的机密性。其原理通常采用分层密钥体系,使用对称加密算法(如AES)加密文件内容,并通过密钥派生函数保护主密钥。这项技术的核心价值在于为敏感数据提供存储级保护,广泛应用于金融、医疗、企业文档管理等对数据保密性要求极高的场景。在移动生态中,Android系统虽然提供了全盘加密和基于文件的加密方案,但对于需要应用层细粒度控制的场景,移植Linux成熟的eCryptfs堆叠式加密文件系统成为了一种可行的技术选择。eCry
Android端DWG解析:libredwg交叉编译与JNI封装实战
在移动端处理工程图纸格式,往往需要复用成熟的C/C++库。NDK交叉编译技术正是连接底层C库与Android系统的关键桥梁,它通过工具链配置、环境变量设定与目标架构编译,将原生库转换为Android可用的静态或动态库。JNI则作为Java层与Native层之间的接口,让Android应用能够直接调用C库的解析能力。这一技术路径广泛应用于工程看图、离线文档处理、数据解析等场景。本文以工程图纸解析为例,详细讲解如何将libredwg这一开源C库通过NDK工具链交叉编译为Android静态库,并封装成JNI接口供
安卓系统文件夹结构及其文件解析
安卓系统文件夹结构及其文件解析详细版本,现在将Android手机内部文件夹的架构和各文件的功能描述介绍给大家,让大家自己手机的内部资源有个比较透彻的理解,同时也避免了用户误删造成的数据丢失和系统崩溃。
解决Android Studio日志太长或滚动太快问题
主要介绍了解决Android Studio日志太长或滚动太快问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧


