获取txt文件的编码格式,出现了非常诡异的问题,深夜求救..

haitai_2013 2012-11-18 12:35:31

当程序执行到mycode = "UTF-8";这一行时,就直接跳到catch 里面,真是太诡异了,我明明就只是给mysode这个字符串变量赋值而已,为什么也会出异常呢?百思不得其解,而且,我读的ANSI编码的txt文件,head[0]、head[1]、head[2]的值根本不是-17、-69、-65,条件根本不满足,为什么还会执行mycode = "UTF-8"这一句呢?真的是极其诡异



package com.example.tyghgy;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import android.util.Log;

public class MyTxt {

private BufferedReader br = null;
InputStream in = null;

public MyTxt(){
File f=null;
f= new File("/sdcard/s.txt");
String sssss;

try {
in = new BufferedInputStream(new FileInputStream(f));
sssss = get_code();
} catch (FileNotFoundException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}

try {
br = new BufferedReader(new InputStreamReader(in, "utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}


private String get_code(){
String mycode = "";
byte[] head = new byte[3];
try{
in.read(head);

if (head[0] == -1 && head[1] == -2 )
mycode = "UTF-16";
if (head[0] == -2 && head[1] == -1 )
mycode = "Unicode";
if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
mycode = "UTF-8";

return mycode;
}catch (Exception e) {
// TODO: handle exception
Log.v("sssssssssss", e.getMessage());
mycode = e.getMessage();
return mycode;
}
}
}
...全文
230 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
haitai_2013 2012-11-20
  • 打赏
  • 举报
回复
问答小助手 2012-11-20
  • 打赏
  • 举报
回复
引用 9 楼 haitai_2013 的回复:
^^^^^^^肿么了
haitai_2013 2012-11-19
  • 打赏
  • 举报
回复
android_Y 2012-11-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/q445697127/article/details/7519750 看看这个
haitai_2013 2012-11-19
  • 打赏
  • 举报
回复
引用 5 楼 yinke22222 的回复:
.................
不简单de 2012-11-19
  • 打赏
  • 举报
回复
gxiaoqiang1987 2012-11-19
  • 打赏
  • 举报
回复
问答小助手 2012-11-19
  • 打赏
  • 举报
回复
你最好把logcat启动日志发一下看看。 用Environment.getExternalStorageDirectory().getAbsolutePath()获取SDCARD的路径:
String sdcardpath = Environment.getExternalStorageDirectory().getAbsolutePath();
String fName = "s.txt";
File f = new File(sdcardpath + File.separator + fName);
//你代码...
注意:还有在manifest中添加下列权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
haitai_2013 2012-11-18
  • 打赏
  • 举报
回复
为什么没人回答、、、、
haitai_2013 2012-11-18
  • 打赏
  • 举报
回复
新八几......

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧