Reputation: 768
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!
Upvotes: 1
Views: 1807
Reputation: 1788
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)
};
}
}
}
}
Upvotes: 1
Reputation: 9492
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;
}
Upvotes: 0
Reputation: 8587
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
}
}
Upvotes: 0
Reputation: 450
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());
}
}
}
Upvotes: 0