0

У меня есть файл wifi2.txt в каталоге файлов моих ресурсов в Android Studio. Тем не менее, я продолжаю получать исключение NULLPointException при попытке получить к нему доступ. Мой код ниже: (спасибо заранее) enter image description hereКак читать текстовый файл из активов в Android Studio?

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

вызывающий 'новый FileInputStream (нуль)', очевидно, вызовет NPE ... – Selvin

+0

Возможные дубликат [Что такое NullPointerException, и как это исправить?] (HTTP: // stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin

ответ

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

Отлично, что сработало –

0

Использование: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

0

метод, чтобы прочитать файл из активов:

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; 
    } 
Смежные вопросы