Java - get files names from directory - java

I have an issue with a Java code where the directory names are being pulled along with the file names. I want to modify the following code to pull only files and not sub-directories from the given directory. I'm a Java newbie so it would be greatly appreciated if someone could answer with the modified code. I have tried a lot of things from past stack overflow answers and just couldn't get it to compile.
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "DirectoryList" as import java.io.*;
import java.sql.*;
public class DirectoryList
{
public static void getList(String directory) throws SQLException
{
File path = new File(directory);
String[] list = path.list();
String element;
for(int i = 0; i < list.length; i++)
{
element = list[i];
#sql {
call Load_File_List_p(:element, :directory)
};
}
}
}
/
Here is what I already tried:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "DirectoryList" as import java.io.*;
import java.sql.*;
public class DirectoryList
{
public static void getList(File directory) throws SQLException
{
File path = new File(directory);
File[] listOfFiles = path.listFiles();
for (File file : listOfFiles)
{
if (file.isFile())
{
for(int i = 0; i < listOfFiles.length; i++)
{
file = listOfFiles[i];
#sql {
call Load_File_List_p(:file, :directory)
};
}
}
}
}
}
/
Thanks!

Only problem with your code is position of extra for loop iteration. Please look below code :
import java.io.*;
import java.sql.*;
public class DirectoryList
{
public static void getList(File directory) throws SQLException {
File path = new File(directory);
File[] listOfFiles = path.listFiles();
for (File file : listOfFiles)
{
if (file.isFile())
{
#sql {
call Load_File_List_p(:file, :directory)
};
}
}
}
}

You have some SQL stuff in there that I removed as the question was about file listings, not SQL. You were pretty close though. Your inner for loop trips you up and needed to be tossed, that was about it. You looped for each file, then for each file you looped again on that file so each file got processed multiple times.
The calls I used have two possible exceptions, so I just put those in the method signature.
public void getList() throws SecurityException, NullPointerException
{
File path = new File("DirectoryName");
File[] listOfFiles = path.listFiles();
if (listOfFiles == null) {
System.out.println("There was an error.");
}
for (File file : listOfFiles)
{
if (file.isFile())
{
System.out.println(file.getName());
}
}
}

The listFiles() method has an overload that takes a FileFilter for your convenience. Combined with lambda, we have:
File [] files = path.listFiles(File::isFile);
if (files != null) {
// listFiles may return null if path is not a directory or there's an IO issue.
for (File f : files) {
// do sth
}
}

Java 8 solution Get all files from current directory:
File path = new File(directory);
File[] list = path.listFiles();
List<File> files = Arrays.stream(list).filter(t->t.isFile()).collect(Collectors.toList());
Get all files including the subfolders:
public static List<File> getList(File directory)
{
File[] list = directory.listFiles();
final List<File> files = Arrays.stream(list).filter(t->t.isFile()).collect(Collectors.toList());
List<File> directories = Arrays.stream(list).filter(t->!t.isFile()).collect(Collectors.toList());
directories.stream().forEach(t->files.addAll(getList(t)));
return files;
}

Related

How find files in a directory knowing a part of the name

I have a problem, i have this directory with 1k+ files and some folders. I need find the path of the files(which are in subdirectories) that starts with "BCM", but not only the first i find but every single file which start with that.
I tried looking at other answers about this topic but i couldn't find help,
tried using this code:
File dir = new File("K:\\Jgencs");
FilenameFilter filter = new FilenameFilter()
{
public boolean accept (File dir, String name)
{
return name.startsWith("BCM");
}
};
String[] children = dir.list(filter);
if (children == null)
{
System.out.println("No directory found");
}
else
{
for (int i = 0; i< children.length; i++)
{
String filename = children[i];
System.out.println(filename);
File h = new File(dir,filename);
System.out.println(h.getAbsolutePath()
[UPDATED] This is how you can achieve using plain Java and filter text from a variable passing as parameter:
Here is my directory: /tmp
And here is the code running:
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GetAllFilesInDirectory {
public static void main(String[] args) throws IOException {
String filter = "BCM";
List<File> files = listFiles("/tmp", new CustomerFileFilter(filter));
for (File file : files) {
System.out.println("file: " + file.getCanonicalPath());
}
}
private static List<File> listFiles(String directoryName, CustomerFileFilter fileFilter) {
File directory = new File(directoryName);
List<File> files = new ArrayList<>();
// Get all files from a directory.
File[] fList = directory.listFiles(fileFilter);
if(fList != null) {
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
files.addAll(listFiles(file.getAbsolutePath(), fileFilter));
}
}
}
return files;
}
}
class CustomerFileFilter implements FileFilter {
private final String filterStartingWith;
public CustomerFileFilter(String filterStartingWith) {
this.filterStartingWith = filterStartingWith;
}
#Override
public boolean accept(File file) {
return file.isDirectory() || file.isFile() && file.getName().startsWith(filterStartingWith);
}
}
This is the output:
file: /private/tmp/BCM01.txt
file: /private/tmp/BCM01
file: /private/tmp/subfolder1/BCM02.txt
Doing recursive calls to the method when finding a directory to also list the files form inside, and filtering by name the files before adding.
You want Files.walk:
try (Stream<Path> files = Files.walk(Paths.get("K:\\Jgencs"))) {
files.filter(f -> f.getFileName().toString().startsWith("BCM")).forEach(
file -> System.out.println(file));
}

Java dir/files list

I am trying to list all directories and files within a certain directory, but while "D:\data" works, "D:\" doesn't. "D" is a secondary disk.
This is the exception:
Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Arrays$ArrayList.<init>(Arrays.java:3813)
at java.util.Arrays.asList(Arrays.java:3800)
at project.1.scavenger.listf(scavenger.java:19)
at project.1.scavenger.listf(scavenger.java:30)
at project.1.scavenger.listf(scavenger.java:30)
at project.1.main(Project1.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Code:
public static List<File> listf(String directoryName) throws IOException {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<File>();
// get all the files from a directory
File[] fList = directory.listFiles();
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
try {
System.out.println(scavenger.checkmime(file.getAbsolutePath()));
} catch (Exception ex) {
}
} else if (file.isDirectory()) {
resultList.addAll(listf(file.getAbsolutePath()));
}
}
// System.out.println(fList);
return resultList;
}
public static String checkmime(String fl) throws MalformedURLException, IOException {
File file = new File(fl);
String mimeType = file.toURL().openConnection().getContentType();
// System.out.println(mimeType);
return mimeType;
}
What's wrong with my code?
Removed error from your version
In your recursion you never ask for null values. Do it and it should run like this:
public static List<File> listf(String directoryName) throws IOException {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<>();
// get all the files from a directory
File[] fList = directory.listFiles();
// this was missing
if (fList == null) {
return null;
}
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
try {
System.out.println(checkmime(file.getAbsolutePath()));
} catch (Exception ex) {
}
} else if (file.isDirectory()) {
// ask here if it was null
List<File> files = listf(file.getAbsolutePath());
if (files != null) {
resultList.addAll(files);
}
}
}
//System.out.println(fList);
return resultList;
}
Why D:\data works and D:\ not
In every root of a drive in a Windows System is a hidden folder structure called $RECYCLE.BIN. In this folder windows stores for each user (sid) an own folder with deleted data (links to it). But a normal user is only allowed to get the first level and not the user folder (with sid value as name).
(German Windows: Papierkorb = Trash)
A maybe much better solution:
A maybe better way of doing such searchings in tree's is to create an Iterator over the directory tree (like a composite iterator). This solution also uses only Java NIO features, so the platform should be changeable (haven't tested!) to for ex. Linux.
DirectoryTreeIterator.java
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class DirectoryTreeIterator implements Iterator<Path> {
private final Deque<Iterator<Path>> deque = new ArrayDeque<>();
public DirectoryTreeIterator(Iterator<Path> it) {
deque.push(it);
}
#Override
public boolean hasNext() {
if (deque.isEmpty()) {
return false;
} else {
Iterator<Path> it = deque.peek();
if (!it.hasNext()) {
deque.pop();
return hasNext();
} else {
return true;
}
}
}
#Override
public Path next() {
if (hasNext()) {
Iterator<Path> it = deque.peek();
Path p = it.next();
try {
// here is the magic recursive on only filtered paths
if (Files.isDirectory(p) && Files.isReadable(p) && !Files.isHidden(p)) {
deque.push(Files.newDirectoryStream(p).iterator());
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return p;
} else {
return null;
}
}
}
Then you are able to use it like a charm:
FileLister.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class FileLister {
public static void main(String[] args) throws IOException {
Path p = Paths.get("D:\\");
Iterator<Path> it = Files.newDirectoryStream(p).iterator();
DirectoryTreeIterator dti = new DirectoryTreeIterator(it);
while (dti.hasNext()) {
Path path = dti.next();
if (!Files.isDirectory(path)) {
String mime = Files.probeContentType(path);
System.out.println("Mime of File "
+ path.getFileName() + " is: " + mime);
}
}
}
}
If you add this code right beneath the directory.listFiles() call:
if (fList == null) {
System.out.println("Couldn't read files in directory: " + directoryName);
return resultList;
}
then you will get a message like this:
Couldn't read files in directory: G:\$RECYCLE.BIN\Windows SID
This Windows SIDs is security identifier for a user on your local machine.
So that means your current user has no permission to read this directory (it belongs to a different user) in the "recycle bin" directory, therefore listFiles() returns null instead of a String[].
You could keep the check and the message I've posted, or you can implement a different way to handle "unreadable" directories.
About why "D:\\" failed and "D:\\data" not:
"D:\\" contains the mentioned "$RECYCLE.BIN" directory with restricted access
"D:\\data" has no directory where your current user isn't allowed to access/read it, therefore listFiles() never returned null.

Recursively file search not working

What i wanna do is to recursively search for some files on the external sd-card. The problem is that the code is looking ok, but (assuming .txt files) it only shows me 7 files out of 100+ that are being spread throughout folders.
The code is this:
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
void makelist(File file){
if(file.isFile()){
if(SimpleAdapter.getFileType(file)==null)
mis.add(file);
else if(SimpleAdapter.getFileType(file).equalsIgnoreCase("text"))
doc.add(file);
}else if(file.isDirectory()){
for(File f:file.listFiles())
makelist(f);
}
}
Any idea how could i make it run correctly?
Assuming you are building two lists (misc files and doc files), try with below code which all all files other than text files to misc and text files to doc.
if(SimpleAdapter.getFileType(file) == null || !SimpleAdapter.getFileType(file).equalsIgnoreCase("text"))
mis.add(file);
else
doc.add(file);
Not sure why it is not working for you. Tried a test program and it worked perfectly...
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileChecker
{
/**
* #param args
*/
public static void main(String[] args) {
SimpleAdapterTest adapter = new SimpleAdapterTest();
adapter.makelist(new File("C:\\MYFolder"));
adapter.showList();
}
}
class SimpleAdapterTest
{
List<File> mis = new ArrayList<File>();
List<File> doc = new ArrayList<File>();
public static String getFileType(File f)
{
String Name = f.getName();
if (f.getName().endsWith(".txt") || Name.endsWith(".TXT")
|| Name.endsWith(".inf") || Name.endsWith(".INF"))
return "text";
return null;
}
public void makelist(File file){
if(file.isFile()){
if(SimpleAdapterTest.getFileType(file)==null)
mis.add(file);
else if(SimpleAdapterTest.getFileType(file).equalsIgnoreCase("text"))
doc.add(file);
}else if(file.isDirectory()){
for(File f:file.listFiles())
makelist(f);
}
}
public void showList()
{
for(File miscFile : mis)
{
System.out.println("Misc files = " + miscFile.getName());
}
for(File docfile : doc)
{
System.out.println("Doc files = " + docfile.getName());
}
}
}

Can't find or load main class

For some reason I am getting an error saying that it can not find or load my main class, can anyone give me a reason for this?
package FindFile;
import java.io.File;
/**
*
* #author Kevin
*/
public class FindFile
{
public void listFilesAndFolders(String directoryName)
{
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for (File file : fList)
{
System.out.println(file.getName());
}
}
public void listFiles(String directoryName)
{
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isFile())
{
System.out.println(file.getName());
}
}
}
public void listFolders(String directoryName)
{
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for(File file : fList)
{
if (file.isDirectory())
{
System.out.println(file.getName());
}
}
}
public void listFilesAndFilesSubDirectories(String directoryName)
{
File directory = new File(directoryName);
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isFile())
{
System.out.println(file.getAbsolutePath());
} else if (file.isDirectory())
{
listFilesAndFilesSubDirectories(file.getAbsolutePath());
}
}
}
public static void main(String[] args)
{
FindFile findFile = new FindFile();
final String directoryWindows = "C:\\test";
findFile.listFiles(directoryWindows);
}
}
Your class is fine, I think your "IDE" (Textpad ?) does not seem to manage compilation and running of java programs.
just look at the location where the compiler writes the class file, this should be something like:
FindFile/FindFile.class
Just go to the directory where the directory FindFile is, open a command line window and run your class like this:
java FindFile.FindFile
I suggest you to use a real IDE, eclipse for instance is a good one for java developing.
The problem could be your project set-up. If you are using an IDE like NetBeans or Eclipse you can define what .java file holds the main function for execution. If that is not set-up it will tell you there is no main function. What Nilesh Tailor tells you might as well be the problem, I haven't worked with Java for some time and I can't fully remember the function constraints regarding class definitions.

Iterate through all files in Java

I want to make my program print huge list of all files that I have on my computer. My problem is that it only prints files from first folder of the first hard-drive, when I want it to print all files located on my computer. Any ideas what am I doing wrong here? Thanks.
Here is code I use:
Main:
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
new Searcher(file.toString().replace('\\', '/')).search();
}
}
}
and Searcher class:
import java.io.File;
public class Searcher {
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
String path = file.getPath().replace('\\', '/');
System.out.println(path);
if (!path.contains(".")) {
new Searcher(path + "/").search();
}
}
}
}
I just tried this and it worked for me. I did have to add one null check and changed the directory evaluation method though:
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Searcher {
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList<File>();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
new Searcher(file.toString().replace('\\', '/')).search();
}
}
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
if(listOfFiles == null) return; // Added condition check
for (File file : listOfFiles) {
String path = file.getPath().replace('\\', '/');
System.out.println(path);
if (file.isDirectory()) {
new Searcher(path + "/").search();
}
}
}
}
You should update your search method like this:
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
String path = file.getPath().replace('\\', '/');
System.out.println(path);
if (file.isDirectory()) {
new Searcher(path + "/").search();
}
}
}
If Java 7 is an option, look into the walkFileTree() method. It will allow you to visit all files and directories in a tree, which you can start from the root of your drive. Just implement a basic FileVisitor to process the file attributes for each Path. You can get started here.
If you're using Java SE 7, use the new file API:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree%28java.nio.file.Path,%20java.util.Set,%20int,%20java.nio.file.FileVisitor%29
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree%28java.nio.file.Path,%20java.nio.file.FileVisitor%29
I don't know what error you are getting but I got a NPE because you are not checking for the null after the following line.
File[] listOfFiles = folder.listFiles();
After changing the code as follows it seemed to run fine , I stopped it because I have a lot of files. I am assuming it will go on to the next root after the first root(c:/ in my case)
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
public class Search {
public static void main(String[] args) {
ArrayList<File> roots = new ArrayList();
roots.addAll(Arrays.asList(File.listRoots()));
for (File file : roots) {
System.out.println(file.toString());
new Searcher(file.toString().replace('\\', '/')).search();
}
}
}
class Searcher {
private String root;
public Searcher(String root) {
this.root = root;
}
public void search() {
System.out.println(root);
File folder = new File(root);
File[] listOfFiles = folder.listFiles();
if(listOfFiles!=null)
{
for (File file : listOfFiles) {
String path = file.getPath().replace('\\', '/');
System.out.println(path);
if (!path.contains(".")) {
new Searcher(path + "/").search();
}
}
}
}
}

Categories