zoukankan      html  css  js  c++  java
  • Android中读取文本文件中内容的方法

        这几天在项目开发中,要读取文本文件中内容的,因此写了个读取文本文件中内容的方法,代码如下:

    //读取文本文件中的内容
        public static String ReadTxtFile(String strFilePath)
        {
            String path = strFilePath;
            String content = ""; //文件内容字符串
                //打开文件
                File file = new File(path);
                //如果path是传递过来的参数,可以做一个非目录的判断
                if (file.isDirectory())
                {
                    Log.d("TestFile", "The File doesn't not exist.");
                }
                else
                {
                    try {
                        InputStream instream = new FileInputStream(file);
                        if (instream != null)
                        {
                            InputStreamReader inputreader = new InputStreamReader(instream);
                            BufferedReader buffreader = new BufferedReader(inputreader);
                            String line;
                            //分行读取
                            while (( line = buffreader.readLine()) != null) {
                                content += line + "\n";
                            }                
                            instream.close();
                        }
                    }
                    catch (java.io.FileNotFoundException e)
                    {
                        Log.d("TestFile", "The File doesn't not exist.");
                    }
                    catch (IOException e)
                    {
                         Log.d("TestFile", e.getMessage());
                    }
                }
                return content;
        }


  • 相关阅读:
    数据结构-查找-有序查找
    发现新大陆 --21lic
    专利检索
    IT行业新闻事件
    流量校准仪开发日志-2017-10-24
    电池充电方案总结
    iOS中创建自定义的圆角按钮
    iOS 内存管理实践
    iOS 内存管理策略
    [置顶] 内存管理一点也不神秘————手绘iOS内存管理细节
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2426891.html
Copyright © 2011-2022 走看看