Store .txt filenames in array on Android - android

I would like to store only .txt filenames from a specific dir (let's say: /sdcard/docs/) into an String array.
Example:
In /sdcard/docs/ there are 5 files: foo.txt, bar.jpg, foofoo.txt, loool.png and foobar.txt. I would like to get array with contents: "foo.txt", "foofoo.txt", "foobar.txt"
How could I do it?

List all files:
https://developer.android.com/reference/java/io/File.html#list%28%29
or
List with filter:
https://developer.android.com/reference/java/io/File.html#list%28java.io.FilenameFilter%29
Just pass your path to the File constructor, and call one of the above methods on it.

Related

Which data structure is implemented to manage files and folders in mobiles?

Trees are generally used for managing data but which particular type of trees are used?
A typical memory representation of a file directory tree looks something like this:
class Directory
string Name
List<Directory> Directories
List<string> Files
Briefly, a Directory contains two types of child nodes: files and directories. You can represent the files as strings, or if you want more information you can create a File class of some kind. A Directory, of course, is a recursive data structure.
To traverse the thing, you write something like this:
TraverseDirectory(Directory d)
output d.Name
for each file in d.Files
output file // it's a string
for each Directory subdir in d.Directories
TraverseDirectory(subdir)
Convert to your favorite language, modify as desired.

Show all .txt files from a folder in an activity

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).
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
In Java 8 you can use Files API and Stream API
Files
.list(Paths.get("."))
.forEach(System.out::println);

Get string from xml (other than strings.xml)

I want to get a string array from other file than strings.xml, because I want my project to be organised.
For example, I have this code:
String[] column1 = getResources().getStringArray(R.array.column1);
But my string array isn't located in strings.xml, but in \res\values\tables\10.xml
How can I get the string array from that file?
Thanks in advance.
Your XML files can have arbitrary names, but they need to be located in the values folder. I suggest just calling your file tables10.xml.

Displaying only names of files in listview

In my app am creating some files and storing them in cache dir. now from app only I want to access this files so i get them in a ListView by giving the path getExternalCacheDir().getAbsolutePath(). Here, I get a ListView with the whole path of the files. Instead I just want the file name to be displayed in list.
Can someone help?
Simply call list() in the diretory to return an array of the file names in that directory:
File directory = getExternalCacheDir();
String[] filenames = directory.list();
Alternatively, you can call listFiles() to get back a File[] if that if more useful to you.
HTH

How To Extract XML Strings From an Android App's Data

I have an app that pulls an XML file from a URL and saves it to its data/data/package.name/file.xml directory. All the XML is is strings. Say I want to reference the String named "restaurant1PhoneNumber". How do I extract it's string value in the XML file?

Categories

Resources