The text.txt file has been created in assets. I check its existence, and writes to me that it does not exist, although it is. What is wrong?

File file = new File("text.txt"); boolean res = file.exists(); String s1 = (res == true) ? ("Да файла") : ("Нет файла"); System.out.println(s1); 

Displays No

  • Arrange the code as text, not a screenshot. - pavlofff

1 answer 1

The files in the assets directory are compiled into the final apk file, so it’s impossible to access them in the way you use. To access the file, use the AssetManager class:

 AssetManager am = getAssets(); InputStream inputStream = am.open("text.txt"); 

Next, do with the file what you need. For example, you can output a file line by line to standard output (taken from here )

 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); 
  • And if I want to know the path to the file from Assets? - V. Ivanova
  • @ V.Ivanova You can use the list method of the am object and get an array of all files located on the specified path. In the array, check the file you are looking for. - Geslot
  • but this only displays file names - V. Ivanova
  • @ V.Ivanova Please specify a little more detailed what the difficulty is - Geslot
  • @ V.Ivanova there is no path to the files in the assett folder, since they are right in the apk file - temq