美文网首页
kotlin 读取本地文件

kotlin 读取本地文件

作者: 安卓_背包客 | 来源:发表于2020-07-17 10:18 被阅读0次

kotlin

   fun mReadTxtFile(strFilePath: String): String? {
        var content = "" //文件内容字符串
        //打开文件
        val file = File(strFilePath)
        //如果path是传递过来的参数,可以做一个非目录的判断
        if (file.isDirectory) {
        } else {
            try {
                val instream: InputStream = FileInputStream(file)
                if (instream != null) {
                    var line: String? = null
                    val buffreader = BufferedReader(
                        InputStreamReader(
                            FileInputStream(file), "UTF-8"
                        )
                    )
                    //分行读取
                    while (buffreader.readLine().also { line = it } != null) {
                        content += "$line;"
                    }
                    instream.close()
                }
            } catch (e: FileNotFoundException) {
                Log.d("TestFile", "The File doesn't not exist.")
            } catch (e: IOException) {
                Log.d("TestFile", e.message!!)
            }
        }
        return content
    }

java


    public static String mReadTxtFile(String strFilePath) {
        String path = String.valueOf(strFilePath);
        String content = ""; //文件内容字符串
        //打开文件
        File file = new File(path);
        //如果path是传递过来的参数,可以做一个非目录的判断
        if (file.isDirectory()) {
        } else {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null) {
                    String line=null;
                    BufferedReader buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
                    //分行读取
                    while ((line = buffreader.readLine()) != null) {
                        content += line+";";
                    }
                    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;
    }
     //过滤为空的值
        val filter = split.filter {  "" != it }
      //去重
            val set = TreeSet(filter)
            val result = mutableListOf<String>()
            result.addAll(set)
        //并集
            mArray?.removeAll(result)
            mArray!!.addAll(list)

相关文章

网友评论

      本文标题:kotlin 读取本地文件

      本文链接:https://www.haomeiwen.com/subject/osgwhktx.html