Initial commit

This commit is contained in:
manfromhuh 2021-08-19 09:35:39 -04:00
commit 88e938e517

View File

@ -0,0 +1,94 @@
/*
* Copyright (C) 2021 manfromhuh <manfromhuh@memeware.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package picture_lister;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
*
* @author manfromhuh <manfromhuh@memeware.net>
*/
public class Picture_Lister {
/**
* A directory path is passed as a string and a String[] is returned containing the paths of all the files matching
* the desired extension. The desired extension or extensions can be pass as a String[].
* @param picture_dir String containing the path to the directory to be queried.
* @param file_types String[] containing the file paths to be parsed for. If it is blank it will use the defaults of
* ".png", ".jpg", and ".gif".
* @return Returns a String[] with the full paths to all files matching the extensions provided in the given directory.
*/
public static String[] list(String picture_dir,String[] file_types) {
if (file_types.length == 0){//Check for input
System.out.println("No input.\nExiting...");
return null;
}
//String picture_dir = file_types[0];
List<String> file_format = new ArrayList<>();
if (file_types.length > 1){//Check if any arguments other than the path have been passed.
file_format.addAll(Arrays.asList(file_types).subList(1, file_types.length));
} else {//The default formats to look for if none are given.
file_format.add(".jpg");
file_format.add(".png");
file_format.add(".gif");
}
System.out.println(picture_dir);
//Creating a File object for directory
File directoryPath = new File(picture_dir);
FilenameFilter picture_filter = (dir, name) -> {//lambda to filter files by extension
if(name.lastIndexOf('.')>0) {
String[] include = file_format.toArray(new String[0]);
// get last index for '.' char
int lastIndex = name.lastIndexOf('.');
// get extension
String str = name.substring(lastIndex);
// match path name extension
for (String place_holder : include) {
if (str.equalsIgnoreCase(place_holder)) {
return true;
}
}
}
return false;
};
if (directoryPath.isDirectory()) {
System.out.println("Listing directory...\n");
//List of all files and directories
File[] contents = directoryPath.listFiles(picture_filter);
System.out.println("List of files and directories in the specified directory:");
String[] file_paths = new String[Objects.requireNonNull(contents).length];
//for (File content : Objects.requireNonNull(contents)) {
for (int i = 0; i < contents.length; i++){
//System.out.println(content);
file_paths[0] = Arrays.toString(contents);
}
return file_paths;
} else {
System.out.println("Not a directory.\nExiting...");
return null;
}
}
}