android读去txt 为string

直接用字节流读取,可保留原格式,在拼装字符串的时候可以把编码转为utf-8 防止乱码,但是根据缓存byte数组的大小不同,会出现部分字符乱码情况

public static String readToText(String filePath) {//按字节流读取可保留原格式,但是有部分乱码情况,根据每次读取的byte数组大小而变化
        StringBuffer txtContent = new StringBuffer();
        byte[] b = new byte[2048];
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            int n;
            while ((n = in.read(b)) != -1) {
                txtContent.append(new String(b, 0, n, "utf-8"));
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return txtContent.toString();
    }

使用字符流的readline读取出来不能保留文档原格式,里面的空格换行都失效了。但是没有乱码。最终解决办法,是采用此方法,然后手动拼接换行符。代码如下:

public static String readtxt(String path) {//按行读取,不能保留换行等格式,所以需要手动添加每行换行符。
//        String result = "";
        StringBuffer txtContent = new StringBuffer();
        File file = new File(path);
        try {
            int len = 0;
            FileInputStream in = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(in, "utf-8");
            BufferedReader br = new BufferedReader(reader);
            String s = null;
            while ((s = br.readLine()) != null) {
                if (len != 0) {// 处理换行符的问题,第一行不换行
                    txtContent.append(new String(("\r\n" + s).getBytes(), "utf-8"));
                } else {
                    txtContent.append(new String(s.getBytes(), "utf-8"));
                }
                len++;
            }
            reader.close();
            in.close();
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    /*try {
        BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        String s = null;
        while((s=br.readLine())!=null){
            result = result + "\n" + s;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/
        return txtContent.toString();
    }
时间: 2024-08-28 06:11:46

android读去txt 为string的相关文章

android.content.res.Resources$NotFoundException:String resource ID #ffffffff

无语,搞了半天,只能去插这个错误代号,结果就找到了这个结果. scoreTextView.setText(score+""); 这个一定要自己手动转换..不科学啊..关键是在eclipse里的提示错误让人以为是id写错了什么的. 以下是别人的一片文章: http://www.blogjava.net/anchor110/articles/355670.html 今天跑程序的时候,出现这样的错误: android.content.res.Resources$NotFoundExcepti

bug android.content.res.Resources$NotFoundException: String resource ID #0x0

android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0 原因分析如下: 遇到这种情况,很有可能是把一个int型业务数据的 设置setText()或者类似的方法中, 这样Android系统就会主动去资源文件当中寻找, 但是它不是一个资源文件ID, 所以就会报出这个bug. 将int型业务数据,转换成String类型即可.

Android 读SIM卡信息

TelephonyManager的应用 手机的最主要功用就是打电话,如果没有电信公司提供的SIM卡,就不能正常地拨打电话,那么,我们有什么方法可以取得SIM卡的状态及相关资料呢? Android API中的TelephonyManager(Android.telephony.TelephonyManager)对象,提供了几个方法可以快速取得SIM卡的状态及相关信息.程序中以getSystemService(TELEPHONY_SERVICE)来取得TelephonyManager对象,以Tele

android.content.res.Resources$NotFoundException: String resource ID #0x11

在练习数据绑定时,出现以下错误 FATAL EXCEPTION: main                                                                               Process: com.vogella.android.databinding, PID: 8332                                                                               andr

异常处理android.content.res.Resources$NotFoundException: String resource ID #0x44

错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如: textView.setText(CharSequence text);

android.content.res.Resources$NotFoundException: String resource ID #0x1

之前忘了记录这个错误,今天又遇到了.唉,人不能纵容自己犯懒,遂记录之. 错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如:

android.content.res.Resources$NotFoundException: String resource ID XXXX

错误:android.content.res.Resources$NotFoundException: String resource ID XXXX 原因:一般发生在参数 int resId 错误,你把String赋值给int的resId,所以编译器找不到正确的resource于是报错. 最简单的例子,检查一下你的Toast.makeText()啊textView.setText啊之类的函数,这种函数通常有几个重载,如: textView.setText(CharSequence text);

异常 android.content.res.Resources$NotFoundException: String resource ID #0x61

09-09 16:08:41.554: E/Weaver(13140):09-09 16:08:41.554: E/Weaver(13140): android.content.res.Resources$NotFoundException: String resource ID #0x6109-09 16:08:41.554: E/Weaver(13140): at android.content.res.Resources.getText(Resources.java:242)09-09 1

Android开发中.toString()和String.valueOf()的区别

其实,很多时候效果是差不多的 但是,有的时候用.toString()会报错,而用String.valueOf()就不会报错 这说明两者是有差别的,在细微之处~ .toString()和String.valueOf()的区别在哪呢? jdk的文档中对.toString()有说明: String.valueOf(Object   o)  if the argument is null, then a string equal to "null ";  otherwise,  the val