A simple useful library for indexing files in java project
Download FileIndexer.jar and add into your project, or you can use it on android project by the following procedure:
- Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Step 2. Add the dependency
dependencies {
implementation 'com.github.sajjad-amin:Java-File-Indexer:v1.0.0'
}
The base class is FileIndexer and it has two static inner class - FileIndexer.FileInfo, FileIndexer.Filter and two method indexAll(), search(). Both methods are return an ArrayList which contains FileIndexer.FileInfo class.
search() accepts two parameters -
- String path
location of directory where you want to search
- String keyword
search() method will match file and directory names containing this keyword and return only matched items
indexAll() accepts four parameters -
-
String
Location of directory where you want to start indexing
-
Boolian
If it true, then it will access all sub directories and their sub directories. Otherwise it will ignore sub directories.
-
Boolean
If it true, then it will accept everything. Otherwise it will ignore hidden directories and files.
-
FileIndexer.Filter
If you want to accept only spacific types of file like- mp4, mp3, dox etc. you can create a Filter and pass on it. Otherwise you can leav it by passing null. for example -
new FileIndexer.Filter(new String[]{"mp4","mp3","dox"})
In FileIndexer.FileInfo class, there are 7 methods -
- getFile()
- Type : File
- Returns File object
- getAbsolutePath()
- Type : String
- Returns absolute path of file
- getName()
- Type : String
- Returns file name
- getSize()
- Type : Long
- Returns total byte of file
- getSizeInKB()
- Type : String
- Returns file size in KB
- getSizeInMB()
- Type : String
- Returns file size in MB
- getSimpleSize()
- Type : String
- Returns file size according to Byte/KB/MB
ArrayList<FileIndexer.FileInfo> list;
FileIndexer indexer = new FileIndexer();
String path = "/home/sayem/Videos";
FileIndexer.Filter filter = new FileIndexer.Filter(new String[]{"mp4","mkv"});
list = indexer.indexAll(path,true,true,filter);
for (FileIndexer.FileInfo info : list){
System.out.println("Path : "+info.getAbsolutePath());
System.out.println("Size : "+info.getSimpleSize());
}
list = indexer.search(path, "movies");
for (FileIndexer.FileInfo info : list){
System.out.println("Path : "+info.getAbsolutePath());
System.out.println("Size : "+info.getSimpleSize());
}