How to read a text file from assets in Android Studio?

梦想与她 提交于 2019-12-23 04:42:17

问题


I have may wifi2.txt file in my assets file directory in Android Studio. However, I keep getting a NULLPointException when I try to access it. My code is below: (Thanks so much in advance)

             //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

回答1:


File file = null;
try {
    FileInputStream is = new FileInputStream(file);

Actually you are not using FileInputStream anywhere. Just use this piece of code

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



回答2:


Method to read a file FROM 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;
    }



回答3:


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

Where wifi2.txt is assumed to reside in 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



来源:https://stackoverflow.com/questions/38121059/how-to-read-a-text-file-from-assets-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!