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;
        }

  • 相关阅读:
    Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
    无向图求点割集的算法
    hdu 2121无根最小树形图要建一个虚拟节点
    hdu 1576扩展欧几里得算法
    欧几里德算法的扩展-求解不定方程
    hdu 3072 强连通+缩点+最小树形图思想
    1352 集合计数 扩展欧几里德算法
    1247 可能的路径 逆向思维
    Atcoder B
    C. Timofey and a tree 观察题 + dfs模拟
  • 原文地址:https://www.cnblogs.com/akira90/p/2763014.html
Copyright © 2011-2022 走看看