get all absolute file path of pdf files in java - java

I need to get all the absolute file path of the files with extension .pdf. I am using the code mentioned below, but I'm able to only get the absolute file path of only one file.
How can I modify the code to get all the absolute file paths ?
public class FindFiles {
String absoluteFilePath = "";
String fileName;
public String PdfFiles(String parentDirectory, String fileExtension) {
FileFilter fileFilter = new FileFilter(fileExtension);
File parentDir = new File(parentDirectory);
// Put the names of all files ending with .pdf in a String array
String[] listOfTextFiles = parentDir.list(fileFilter);
if (listOfTextFiles.length == 0) {
System.out.println("There are no files in this direcotry!");
}
for (String file : listOfTextFiles) {
//construct the absolute file paths...
absoluteFilePath = new StringBuffer(parentDirectory).append(File.separator).append(file).toString();
fileName = file.toString();
}
return absoluteFilePath;
}
public static void main(String args[]) {
FindFiles f = new FindFiles();
f.PdfFiles("", "");
}
}

you are overrding absoluteFilePath every time in the loop;
try with
absoluteFilePath += new StringBuffer(parentDirectory).append(File.separator).append(file).toString();

Don't be bothered, use java.nio.file:
final Path dir = Paths.get(baseDir).toAbsolutePath();
final String filter = "*." + extension;
final List<Path> ret = new ArrayList<>();
try (
final DirectoryStream<Path> dirstream
= Files.newDirectoryStream(dir, filter);
) {
for (final Path entry: dirstream)
ret.add(entry);
}
return ret;
If you use Java 8, it's even more simple.

A more efficient solution for your problem can be given using classes in java.nio package.
E.g. For checking whether a file is a pdf file or not, use Files.probeContentType(Path path).
Instead of writing loops to visit all directories and files inside these directories, use Files.walkFileTree(Path start, FileVisitor<? super Path> visitor)
Solution for your problem using these classes are
public class FindPdfFiles {
public static void main(String[] args) throws IOException{
final Path path = Paths.get("C:\\SearchDirectoryForPDF");
Files.walkFileTree(path, new FindPdfFilesFilter());
}
}
class FindPdfFilesFilter extends SimpleFileVisitor<Path> {
#Override
public FileVisitResult visitFile(Path path, BasicFileAttributes arg1)
throws IOException {
final String mimeTypeOfFile = Files.probeContentType(path);
if(mimeTypeOfFile != null && !mimeTypeOfFile.isEmpty() && mimeTypeOfFile.toLowerCase().contains("pdf")) {
System.out.println(path.toString());
}
return FileVisitResult.CONTINUE;
}
}

Related

Java, search files of given pattern and get the directory names and complete filenames

I am new to Java. Looking for code to search for files with .ofg extension in all the sub-directories of /var/data.
The desired outputs are
the subdirectory name(s), which has the files with those files
the full names of the files
the number of those files in that subdirectory.
There are some tutorials available, but nothing i could find fitting to my code base; like
public class FindFiles {
int inProcThreshold = 0;
protected File recurfile(File file) {
File[] dirlist = file.listFiles();
for (File f : dirlist) {
if (f.isDirectory()) {
return f;
}
}
return null;
}
protected int numOfInProcs(String location, int level, int maxdepth) {
File base = new File(location);
File[] firstlevelfiles = base.listFiles();
while (level <= maxdepth) {
for (File afile : firstlevelfiles) {
if (afile.isDirectory()) {
base = recurfile(afile);
} else {
if (afile.getName().endsWith(".txt")) {
inProcThreshold++;
}
}
}
level++;
}
return inProcThreshold;
}
public static void main(String[] args) {
FindFiles test = new FindFiles();
String dirToList = "I:\\TEST-FOLDER";
String ext = ".txt";
int count = test.numOfInProcs(dirToList, 0, 10);
System.out.println("Number of txt files are " + count);
}
}
This is the code I am trying but it returns 0 as output to me. I am trying to search for files with extension.txt in the I:\TEST-FOLDER subfolders.
Use this filter by giving directory addres in dirName Parameter it will list all directories with extension .ofg
import java.io.File;
import java.io.FilenameFilter;
public class Filter {
public File[] finder( String dirName){
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename)
{ return filename.endsWith(".ofg"); }
} );
}
}
I think what you are looking for is Files.find. Pass it a Predicate which checks that path.toString().endsWith(".ofg"),
It will return a Stream of Path objects representing the matching files. You can extract all the data you want by iterating on this Stream.
If you are not required to write the recursive part yourself (for practice or as task), you could use Files#walkFileTree with a custom implementation of the FileVisitor Interface (As #Mena proposed in his comment).
Extend the SimpleFileVisitor class (or implement the FileVisitor interface) and provide your code to be executed on each file:
public class OfgFolderCollectingFileVisitor extends SimpleFileVisitor<Path> {
/** Stores the matching file paths */
private final List<Path> collectedPaths = new LinkedList<>();
#Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
// check if the current file is an .ofg file
if (file.toString().endsWith(".ofg")) {
// it is -> add it's containing folder to the collection
this.collectedPaths.add(file.getParent());
}
return super.visitFile(file, attrs);
}
public List<Path> getCollectedPaths() {
return this.collectedPaths;
}
}
Then pass an instance of your implementation to Files#walkFileTree and check the collected paths afterwards:
final OfgFolderCollectingFileVisitor visitor = new OfgFolderCollectingFileVisitor();
try {
Files.walkFileTree(Paths.get("/var/data"), visitor);
} catch (final IOException ex) {
ex.printStackTrace();
return;
}
// let's see if something matched our criteria
final List<Path> ofgContainers = visitor.getCollectedPaths();
System.out.printf("Files found: %d%n", ofgContainers.size());
if (!ofgContainers.isEmpty()) {
System.out.printf("%nContaining directories:%n");
for (final Path ofgContainer : ofgContainers) {
System.out.printf("- %s%n", ofgContaininer);
}
}
Here is some example output (yes, folder2 and it's subfolder contain an .ofg file)
Files found: 3
Containing directories:
- \var\data\folder1\folder1.1
- \var\data\folder2
- \var\data\folder2\folder2.2

Searching Large Directories for Multiple Folders - JAVA

I want to be able to search a large directory, including all subfolders for every folder called "online" and add the folders once found to a list.
So in other words: Is this file a folder? if not ignore, if yes is this folder called "online"?, if yes add to list, if not open and cycle through folder contents and start again.
I have a script:
String fileType = "online";
private void buildList(File aFile) {
if (aFile.isDirectory()) {
if (fileName.contains(fileType)) {
addToList(aFile);
} else {
for (File bFile : aFile.listFiles()) {
buildList(bFile);
}
}
}
}
This works fine on small directories but not on large directories due to the large number of arrays it has open. It hangs and uses up all the memory.
I'm open to all suggestions. JAVA only and ideally JDK1.6 compatible. Thank you very much in advance!!!!
EDIT:
static long counter = 0L;
ArrayList<File> opFolders;
public final class DirectoryCollectorVisitor extends SimpleFileVisitor<Path> {
private final List<Path> list;
public DirectoryCollectorVisitor(final List<Path> list) {
this.list = list;
}
#Override
public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes attrs) {
counterPrintField.setText("" + counter++);
if (path.getFileName().toString().contains("online")) {
list.add(path);
File aFile = path.toFile();
opFolders.add(aFile);
}
return FileVisitResult.CONTINUE;
}
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = jFileChooser1;
chooser.setCurrentDirectory(new java.io.File("O:\\Prod\\Clients"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == chooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
jTextArea1.setText(null);
opFolders = new ArrayList<>();
final Path baseDir = Paths.get(file.getAbsolutePath());
final List<Path> dirList = new ArrayList<>();
dirList.add(baseDir);
final FileVisitor<Path> visitor = new DirectoryCollectorVisitor(dirList);
Files.walkFileTree(baseDir, visitor);
if(opFolders.isEmpty()){
System.err.println("EMPTY");
}
for (File aFile : opFolders) {
if (!aFile.isDirectory() && !aFile.getName().toLowerCase().endsWith(".db")) {
jTextArea1.append(aFile.getAbsolutePath() + "\n");
}
}
jTextArea1.append("...COMPLETE...");
} catch (Exception ex) {
System.out.println("Problem accessing directory: " + file.getAbsolutePath());
}
} else {
System.out.println("File access cancelled by user.");
}
}
#fge updated code
Unfortunately you say you are limited to Java 6. Given that requirement there is no way to use the File API to do that. .listFiles() can only populate directory entries in an eager fashion.
Well, there would be one way... Provided you use a Unix like operating system you could use a ProcessBuilder and issue a process from the base directory with a command such as find -type d -name online:
final File baseDir = ...;
final ProcessBuilder pb = new ProcessBuilder(
"find", "-type", "d", "-name", "online"
);
pb.directory(baseDir);
final Process p = pb.start();
// use the Process' InputStream
If you use Java 7+ you have an easier time; program a FileVisitor to collect into a list:
public final class DirectoryCollectorVisitor
extends SimpleFileVisitor<Path>
{
private final List<Path> list;
public DirectoryCollectorVisitor(final List<Path> list)
{
this.list = list;
}
#Override
public FileVisitResult previsitDirectory(final Path path, final BasicFileAttributes attrs)
{
if (path.getFileName().toString().equals("online"))
list.add(path);
return FileVisitResult.CONTINUE;
}
}
// ...
final Path baseDir = Paths.get(...);
final List<Path> dirList = new ArrayList<>();
final FileVisitor<Path> visitor = new DirectoryCollectorVisitor(list);
Files.walkFileTree(baseDir, visitor);
// dirList is not filled with the entries
With Java 8, it's even more simple:
private static final BiPredicate<Path, BasicFileAttributes> ONLINE_DIRS
= (path, attrs) -> attrs.isDirectory()
&& path.getFileName().toString().equals("online");
// ...
final Path baseDir = Paths.get(...);
final List<Path> dirList;
try (
final Stream<Path> stream = Files.find(baseDir, Integer.MAX_VALUE,
ONLINE_DIRS);
) {
dirList = stream.collect(Collectors.toList());
}

Search for file of a specific pattern in a directory

In Java, how do I check folders recursively for a file of a specific pattern? I've seen the below code in a lot of posts online:
public static void findFiles() throws IOException {
File dir = new File(".");
FileFilter fileFilter = new WildcardFileFilter("*.txt");
File[] files = dir.listFiles(fileFilter);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
}
In my application, I basically need to check files matching *.txt in a user's home directory.
Since the path will vary for each user, how do I use this code to search for a file matching a pattern anywhere on the filesystem?
You could actually simply use:
final File dir = new File(System.getProperty("user.home"));
This would set the dir to your user's home directory. From there, you simply have to list all the .txt files, not recursively ;)
However, if you still want to list all files from a directory, recursively, you can use the following method:
public static List<File> walk(final File root, final String extension) {
final File[] list = root.listFiles();
if (list == null) {
return Collections.EMPTY_LIST;
}
final List<File> res = new ArrayList<>();
for (final File f : list) {
if (f.isDirectory()) {
res.addAll(walk(f, extension));
} else {
if (f.getName().endsWith(extension)) {
res.add(f);
}
}
}
return res;
}
You can use it as follows:
public static void main(final String[] args) {
for (final File file : walk(new File("/home/user3751169"), ".txt")) {
System.out.println(file.getAbsolutePath());
}
}
On the other hand, if you want to look only for the files in the home directory of the current user, you should remove the recursive call to walk():
public static List<File> walk(final File root, final String extension) {
final File[] list = root.listFiles();
if (list == null) {
return Collections.EMPTY_LIST;
}
final List<File> res = new ArrayList<>();
for (final File f : list) {
if (f.isFile() && f.getName().endsWith(extension)) {
res.add(f);
}
}
return res;
}

how to get/ make file object in the program given below

i am a nibble in java. i have my own efforts to get the things done. but certainly i am facing a challenge. i have a dummy program that searches for files of a particular extension(.txt) supplied as a command line argument. i am trying to make file objects of these searched file for further manipulations. but i can't understand how to do this in my code.. here is my code sample...
public class Find {
public static class Finder extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private int numMatches = 0;
Finder(String pattern) {
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
numMatches++;
System.out.println(file);
}
}
// Prints the total number of
// matches to standard out.
void done() {
System.out.println("Matched: "+ numMatches);
}
// Invoke the pattern matching
// method on each file.
//#Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}
// Invoke the pattern matching
// method on each directory.
//#Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
find(dir);
return CONTINUE;
}
//#Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.err.println(exc);
return CONTINUE;
}
}
public static void main(String[] args) throws IOException {
Iterable<Path> root;
root = FileSystems.getDefault().getRootDirectories();
// System.out.println(name.getAbsolutePath());
for (Path startingDir : FileSystems.getDefault().getRootDirectories()) {
String pattern = args[0];
Finder finder = new Finder(pattern);
Files.walkFileTree(startingDir, finder);
}
}
}
here is what i am trying to do. the output of my program is a long list of text files with their absolute path. now i want to make objects of these files so that i can upload these to a URL. to upload them i have to make a stream with file object to be sent..how to get absoluteFilename?? to get this you must have a file object...right.... and my revised question is : how to make file objects of searched files???
FileInputStream fileInputStream = null;
try {
new FileInputStream("absoluteFilename");
byte[] buffer = new byte[MAX_SIZE];
int bufferIndex = 0;
while (fileInputStream.available() > 0) {
buffer[bufferIndex++] = (byte) fileInputStream.read();
}
byte[] fileContent = new byte[bufferIndex];
System.arraycopy(buffer,0,fileContent,0,bufferIndex);
URL serverUrl = new URL(url);
URLConnection connection = serverURL.openConnection();
connection.setConnectTimeout(60000);
connection.getOutputStream().write(fileContent);
} catch (Exception fatal) {
//proper handling??

delele all files with an extension in java

So I found some code earlier that looks like it would work but it doesn't call to delete the files just to list them. What do I need to add so that it deletes the files?
import java.io.File;
import java.util.regex.Pattern;
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static void walkDir(final File dir, final Pattern pattern) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
walkDir(file, pattern);
} else if (pattern.matcher(file.getName()).matches()) {
System.out.println("file to delete: " + file.getAbsolutePath());
} } } }
public static void main(String[] args) {
walkDir(new File(userprofile+"/Downloads/Software_Tokens"),
Pattern.compile(".*\\.sdtid"));
}
}
Once you have the path to the file, delete him:
File physicalFile = new File(path); // This is one of your file objects inside your for loop, since you already have them just delete them.
try {
physicalFile.delete(); //Returns true if the file was deleted or false otherwise.
//You might want to know this just in case you need to do some additional operations based on the outcome of the deletion.
} catch(SecurityException securityException) {
//TODO Handle.
//If you haven't got enough rights to access the file, this exception is thrown.
}
To delete a file you can call the delete function
file.delete();
You can invoke the delete() method on an instance of File. Be sure to check the returncode to make sure your file was actually deleted.
Use file.delete(); to delete a file.
You need to learn Java basics properly before attempting to write programs. Good resource: http://docs.oracle.com/javase/tutorial/index.html
Call File.delete() for each file you want to delete. So your code would be:
import java.io.File;
import java.util.regex.Pattern;
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static void walkDir(final File dir, final Pattern pattern) {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory()) {
walkDir(file, pattern);
} else if (pattern.matcher(file.getName()).matches()) {
System.out.println("file to delete: " + file.getAbsolutePath());
boolean deleteSuccess=file.delete();
if(!deleteSuccess)System.err.println("[warning]: "+file.getAbsolutePath()+" was not deleted...");
}
}
}
}
public static void main(String[] args) {
walkDir(new File(userprofile+"/Downloads/Software_Tokens"),
Pattern.compile(".*\\.sdtid"));
}
}
final File folder = new File("C:/Temp");
FileFilter ff = new FileFilter() {
#Override
public boolean accept(File pathname) {
String ext = FilenameUtils.getExtension(pathname.getName());
return ext.equalsIgnoreCase("EXT"); //Your extension
}
};
final File[] files = folder.listFiles(ff);
for (final File file : files) {
file.delete();
}
public class cleardir {
static String userprofile = System.getenv("USERPROFILE");
private static final String FILE_DIR = userprofile+"\\Downloads\\Software_Tokens";
private static final String FILE_TEXT_EXT = ".sdtid";
public static void run(String args[]) {
new cleardir().deleteFile(FILE_DIR,FILE_TEXT_EXT);
}
public void deleteFile(String folder, String ext){
GenericExtFilter filter = new GenericExtFilter(ext);
File dir = new File(folder);
if (dir.exists()) {
//list out all the file name with .txt extension
String[] list = dir.list(filter);
if (list.length == 0) return;
File fileDelete;
for (String file : list){
String temp = new StringBuffer(FILE_DIR)
.append(File.separator)
.append(file).toString();
fileDelete = new File(temp);
boolean isdeleted = fileDelete.delete();
System.out.println("file : " + temp + " is deleted : " + isdeleted);
}
}
}
//inner class, generic extension filter
public class GenericExtFilter implements FilenameFilter {
private String ext;
public GenericExtFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return (name.endsWith(ext));
}
}
}

Categories

Resources