2

I have a application where the user can write a text into a EditText and the written text is saved in an auto generated .txt file.

Now I want to show all the txt files from the folder in an activity, not like a regular file browser, just the title of the .txt files in a ListView or as a TextView (each file is illustrated as a TextView).

Community
  • 1
  • 1
Baran
  • 57
  • 3

2 Answers2

1

Get the path of that directory as string and create a new file with this path then,

String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder"; 

File file=new File(path);

or

File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"..../yourFolder");

Use file.list which will give u all files name in form of an array then use a for loop with endsWith function and store data into a list like this

String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
    if(i.endsWith(".txt")){
       l.add(i);
    }
}

you will have names all text files under selected directory in list l

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

In Java 8 you can use Files API and Stream API

Files
    .list(Paths.get("."))
    .forEach(System.out::println);
Vadeg
  • 1,156
  • 10
  • 22