Object-Oriented library for working with file tree.
<dependency>
<groupId>com.github.viise.papka</groupId>
<artifactId>papka</artifactId>
<version>1.0.2</version>
</dependency>
implementation 'com.github.viise.papka:papka:1.0.2'
You have a raw filename list:
/root1.png
/root2.png
/root3.png
/music/a.mp3
/music/opus/oA.mp3
/music/opus/oB.mp3
/music/opus/bth/1.mp3
/music/opus/bth/111.mp3
/music/opus/mzt/21.mp3
/music/opus/bch/13.flac
/doc/table.docx
You can use Folder
interface:
Folder<String> root = new FolderRawText(
"/root1.png",
"/root2.png",
"/root3.png",
"/music/a.mp3",
"/music/opus/oA.mp3",
"/music/opus/oB.mp3",
"/music/opus/bth/1.mp3",
"/music/opus/bth/111.mp3",
"/music/opus/mzt/21.mp3",
"/music/opus/bch/13.flac",
"/doc/table.docx");
Now, you can work with this object.
List<String> files = root.files();
// |
// |-> root1.png
// |-> root2.png
// +-> root3.png
List<Folder<String>> children = root.children();
// |
// |-> Folder("/music")
// +-> Folder("/doc")
List<Folder<String>> children = root.children();
Folder<String> child = children.get(0);
child.fullName(); // --> /music
List<Folder<String>> children = root.children();
Folder<String> child = children.get(0);
child.shortName(); // --> music
root.travel(folder -> {
String shortName = folder.shortName();
String fullName = folder.fullName();
List<String> files = folder.files();
List<Folder<String>> children = folder.children();
});
Papka creates the following file structure:
/-------------
| |
| |
| +--> root1.png
| +--> root2.png
| +--> root3.png
\/
+-------------------+
| |
\/ \/
music doc
| | |
\/ | |
opus +--> a.mp3 +--> table.docx
| |
| |
| +--> oA.mp3
| +--> oB.mp3
|
\/
+-----------------------------+
| | |
\/ \/ \/
bch bth mzt
| | |
| | |
+--> 13.flac +--> 1.mp3 +--> 21.mp3
+--> 111.mp3
Method travel()
allows you to do a complete traversal of the file tree, starting at the root.
In our case, travel()
starts in folder /
(root). Then it moves to doc
,
music
, opus
, bch
, bth
, and the end is mzt
.
Thus, method travel()
traverses the entire file tree.
Search is carried out through the Search
interface.
Search files
Search<List<String>, String> search = new SearchFilesByRegex(root);
List<String> files = search.answer("a.mp3") // {a.mp3}
Search files by extension
Search<List<String>, String> search = new SearchByExt<>(
new SearchFilesByRegex(root));
List<String> files = search.answer(".flac") // {13.flac}
Search files by start with
Search<List<String>, String> search = new SearchByStartWith<>(
new SearchFilesByRegex(root));
List<String> files = search.answer("1") // {1.mp3, 111.mp3, 13.flac}
Search folders
SearchFolders<String, String> search = new SearchFoldersByRegex<>(root);
List<Folder<String>> folders = search.answer("^b.*$) // {bch, bth}
Search folder by full name
SearchFolder<String, String> search = new SearchFolderByFullName<>(root);
Folder<String> folder = search.answer("/music/opus/mzt") // mzt
Search folder by short name
SearchFolder<String, String> search = new SearchFolderByShortName<>(root);
Folder<String> folder = search.answer("doc") // doc
The interface Filter
provides filtering. It differs from Search
in that it does not throws
exceptions.
Suppose we have the following raw list of files
:
/music/sound1.mp3
/music/sound1.mp3
/music/sound2.mp3
/music/sound2.mp3
If using Search
:
Search<List<String>, List<String>> search = new SearchUniqueByList();
List<String> uqFiles = search.answer(files); // throw NotFoundException
If using Filter
:
Filter<List<String>> filter = new FilterFilesRaw(
new SearchUniqueByList<>(),
files);
List<String> uqFiles = filter.apply(); // return empty List
Also Filter
can be used for folders:
Filter<List<Folder<String>>> filter = new FilterFolders<>(
new SearchByStartWith<>(
new SearchFoldersByRegex<>(
root)),
"b");
List<Folder<String>> folders = filter.apply(); // {bch, bth}
Suppose we have the following raw list of files
:
/music/sound1.mp3
/music/sound1.mp3
/music/sound2.mp3
/music/sound2.mp3
We want to keep one value for each duplicate. We can use FilterFilesUniqueNormalize
:
Filter<List<String>> filter = new FilterFilesUniqueNormalize<>(files);
List<String> normFiles = filter.apply(); // {/music/sound1.mp3, /music/sound2.mp3}
Papka supports working with filesystem.
For unix like system:
Folder<File> folder = new FolderFile(
"/home/papka/file1.txt",
"/home/papka/file2.txt",
"/home/papka/file3.txt"
);
For Windows:
Folder<File> folder = new FolderFileWin(
"C:\\papka\\file1.txt",
"C:\\papka\\file2.txt",
"C:\\papka\\file3.txt"
);
In Windows implementation, the root folder is the same as in Unix implementation, because such a case is possible:
Folder<File> folder = new FolderFileWin(
"C:\\papka\\file1.txt",
"C:\\papka\\file2.txt",
"C:\\papka\\file3.txt",
"D:\\papka\\dFile1.txt",
"D:\\papka\\dFile2.txt"
);
Then folder.fullName()
is /
, and folder.children()
is folder /\\C:
and /\\D:
.
You can remove all non existing files in filesystem use excludeNonExisting
parameter:
boolean excludeNonExisting = true;
Folder<File> folder = new FolderFileWin(
excludeNonExisting,
"C:\\papka\\file1.txt",
"C:\\papka\\file2.txt",
"C:\\papka\\file3.txt",
"D:\\papka\\dFile1.txt",
"D:\\papka\\dFile2.txt"
);
In Windows implementation you can use unix like files:
boolean excludeNonExisting = true;
boolean isUnixLike = true;
Folder<File> folder = new FolderFileWin(
excludeNonExisting,
isUnixLike,
"/papka/file1.txt",
"/papka/file2.txt",
"/C:/papka/file3.txt",
"D:/papka/dFile1.txt",
"D:/papka/dFile2.txt"
);
If the drive is not specified in the file name, then system drive will be used by default.
You can use Search
interface to do this:
String beginWith = "/home"; // Files will be searched from here
Search<List<File>, String> search = new SearchFilesInSystem(beginWith);
List<File> files = new SearchByExt<>(search).answer(".txt"); // Find all files in folder '/home' with extension '.txt'
String beginWith = "/home";
Search<Folder<File>, String> search = new SearchFolderInSystem(name);
Folder<File> folder = search.answer("documents"); // Find folder with name documents
String beginWith = "/home";
Search<List<File>, String> search = new SearchFilesByFolderNameInSystem(beginWith);
List<File> files = search.answer("documents"); // Find all files in folder 'documents'
For more information, read the docs wiki.