android - 如何从 Android Studio 中的 Assets 中读取文本文件?

我在 Android Studio 的 Assets 文件目录中可能有 wifi2.txt 文件。但是,当我尝试访问它时,我不断收到 NULLPointException。我的代码如下:(提前致谢)

             //CSV FILE READING
    File file = null;



    try {



        FileInputStream is = new FileInputStream(file);

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("wifi2.txt")));
            String line;
            Log.e("Reader Stuff",reader.readLine());
            while ((line = reader.readLine()) != null) {
                Log.e("code",line);
                String[] RowData = line.split(",");
                LatLng centerXY = new LatLng(Double.valueOf(RowData[1]), Double.valueOf(RowData[2]));
                if (RowData.length == 4) {
                    mMap.addMarker(new MarkerOptions().position(centerXY).title(String.valueOf(RowData[0]) + String.valueOf(RowData[3])).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                }

            }

        } catch (IOException ex) {
           ex.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


   //Done with CSV File Reading

最佳答案

在 Kotlin 中,我们可以实现这一点:-

val string = requireContext().assets.open("wifi2.txt").bufferedReader().use {
                it.readText()
            }

https://stackoverflow.com/questions/38121059/

相关文章:

android - 完成其他 Activity 之前的所有 Activity

bash - 在 byobu 选项卡中启动命令的脚本

selenium - java.lang.NoClassDefFoundError : freema

laravel - 仅在 Laravel 中公开某些授权路由

php - 在 Laravel 迁移中更改列类型的最佳方法是什么?

google-api - Google Classroom API 修改附件

python - 找到坡度最陡的点 python

android - JSONObject.put(string,string) 不工作

ruby - 符号有什么用?

asp.net-mvc - 需要日期时间(+18 年)