java读取文本

32 篇文章 2 订阅

读写文本文件,采用Reader是非常方便,如FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader, 它是字节转换为字符的桥梁。可以在构造方法里指定编码的方式,若不指定,将采用底层操作系统的默认编码方式,例如GBK等。read()方法是比较耗费时间的,如果为了提高效率我们可以使用BufferedReader对Reader进行包装,这样可以提高读取得速度,我们可以一行一行的读取文本,使用readLine()方法。


    /**
     * 以行为单位读取文件,常用于读面向行的格式化文件
     *
     * @param fileName 文件完整路径
     * @return 返回读取的文本
     */
    public static String readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuilder body = new StringBuilder();
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                body.append(tempString);
                body.append("\n");//换行符
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            //读完记得关闭流
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return body.toString();
    }

    /**
     * Android中读取资源文件
     *
     * @param context
     * @param resId   资源文件ID
     * @return 返回资源文件中文本
     */
    private static final String readTextByLine(Context context, int resId) {
        StringBuilder body = new StringBuilder();
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            //resId写在openRawResource();的参数列表
            is = context.getResources().openRawResource(R.raw.mytext);
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String temp = null;
            while ((temp = br.readLine()) != null) {
                body.append(temp);
                body.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return body.toString();
    }

String name = “AAAA.txt”;
String lujing = “1”+”/”+”2”;//定义路径
File a = new File(lujing,name);

a.getParentFile().mkdirs(); //这里如果不加getParentFile(),创建的文件夹为”1/2/AAAA.txt/”

那么,a的意义就是“1/2/AAAA.txt”。

这里a是File,但是File这个类在Java里表示的不只是文件,虽然File在英语里是文件的意思。Java里,File至少可以表示文件或文件夹(大概还有可以表示系统设备什么的,这里不考虑,只考虑文件和文件夹)。

也就是说,在“1/2/AAAA.txt”真正出现在磁盘结构里之前,它既可以表示这个文件,也可以表示这个路径的文件夹。那么,如果没有getParentFile(),直接执行a.mkdirs(),就是说,创建“1/2/AAAA.txt”代表的文件夹,也就是“1/2/AAAA.txt/”,在此之后,执行a.createNewFile(),试图创建a文件,然而以a为名的文件夹已经存在了,所以createNewFile()实际是执行失败的。你可以用System.out.println(a.createNewFile())这样来检查是不是真正创建文件成功。

所以,这里,你想要创建的是“1/2/AAAA.txt”这个文件。在创建AAAA.txt之前,必须要1/2这个目录存在。所以,要得到1/2,就要用a.getParentFile(),然后要创建它,也就是a.getParentFile().mkdirs()。在这之后,a作为文件所需要的文件夹大概会存在了(有特殊情况会无法创建的,这里不考虑),就执行a.createNewFile()创建a文件。

参考http://blog.csdn.net/jiangxinyu/article/details/7885518/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值