【问题标题】:How to read a text file from assets in Android Studio?如何从 Android Studio 中的资产中读取文本文件?
【发布时间】:2016-06-30 10:54:47
【问题描述】:

我在 Android Studio 的资产文件目录中可能有 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

【问题讨论】:

标签: android android-studio nullpointerexception android-assets


【解决方案1】:

在 Kotlin 中,我们可以做到这一点:-

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

【讨论】:

  • 结果如何?
【解决方案2】:
File file = null;
try {
    FileInputStream is = new FileInputStream(file);

实际上,您并没有在任何地方使用 FileInputStream。就用这段代码

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

【讨论】:

    【解决方案3】:

    从assets读取文件的方法:

        public static String readFile(AssetManager mgr, String path) {
            String contents = "";
            InputStream is = null;
            BufferedReader reader = null;
            try {
                is = mgr.open(path);
                reader = new BufferedReader(new InputStreamReader(is));
                contents = reader.readLine();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    contents += '\n' + line;
                }
            } catch (final Exception e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException ignored) {
                    }
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException ignored) {
                    }
                }
            }
            return contents;
        }
    

    【讨论】:

      【解决方案4】:

      用法: String yourData = LoadData("wifi2.txt");

      假设 wifi2.txt 位于assets

       public String LoadData(String inFile) {
              String tContents = "";
      
          try {
              InputStream stream = getAssets().open(inFile);
      
              int size = stream.available();
              byte[] buffer = new byte[size];
              stream.read(buffer);
              stream.close();
              tContents = new String(buffer);
          } catch (IOException e) {
              // Handle exceptions here
          }
      
          return tContents;
      
       }
      

      Reference

      【讨论】:

        【解决方案5】:

        我使用 kotlin 从资产文件加载文本的解决方案

        object AssetsLoader {
        
            fun loadTextFromAsset(context: Context, file: String): String {
                return context.assets.open(file).bufferedReader().use { reader ->
                    reader.readText()
                }
            }
        
        }
        

        像这样使用它:

        val text = AssetsLoader.loadTextFromAsset(context, "test.json")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-02
          • 2015-01-21
          • 1970-01-01
          • 1970-01-01
          • 2018-01-19
          相关资源
          最近更新 更多