Listing all files with a .txt extension recursively - java

I am trying to recursively search the directory and list all .txt files found. This is my code for it:
private static void listFilesForFolder(File folder) throws FileNotFoundException {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(Arrays.toString(fileEntry.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
})));
}
}
}
I'm using FileFilter to print out all the .txt files but it prints out null instead. Anyone know why that's the case?

I think instead of using a FileFilter in the else block, you can simply use an if statement. Because in this else block you always have a file (not a directory). See whether below change works for you.
private static void listFilesForFolder(File folder) throws FileNotFoundException
{
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
if (fileEntry.getPath().toLowerCase().endsWith(".txt")) {
System.out.println(fileEntry.getPath());
}
/*System.out.println(Arrays.toString(fileEntry.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
})));*/
}
}
}
EDIT:
If you really want to do this using FileFilter, you can do it like this:
private static void listFilesForFolder2(File folder) throws FileNotFoundException
{
File[] textFiles = folder.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getName().toLowerCase().endsWith(".txt");
}
});
if (textFiles != null) {
for (File f : textFiles) {
System.out.println(f.getPath());
}
}
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder2(fileEntry);
}
}
}

The method file.listFiles() return String[] only when the file object is a Directory object,if file is a File object it will return null. You can just modify you code like this:
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
if (fileEntry.getName().endsWith(".txt")) {
System.out.println(fileEntry.getName());
}
}
}
if you can use apache commons-io it will be easy to use IOFileFilter to filter what you want:
FileUtils.listFiles(folder, new IOFileFilter() {
#Override
public boolean accept(File file) {
return file.getName().endsWith(".txt");
}
#Override
public boolean accept(File file, String s) {
return true;
}
}, new IOFileFilter() {
#Override
public boolean accept(File file) {
return true;
}
#Override
public boolean accept(File file, String s) {
return true;
}
}).stream().forEach(file -> System.out.println(file.getName()));

Related

How to get dynamic name of .zip file after download in JAVA [duplicate]

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
How to read all the files in a folder through Java? It doesn't matter which API.
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);
Files.walk API is available from Java 8.
try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) {
paths
.filter(Files::isRegularFile)
.forEach(System.out::println);
}
The example uses try-with-resources pattern recommended in API guide. It ensures that no matter circumstances the stream will be closed.
File folder = new File("/Users/you/folder/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
}
}
In Java 8 you can do this
Files.walk(Paths.get("/path/to/folder"))
.filter(Files::isRegularFile)
.forEach(System.out::println);
which will print all files in a folder while excluding all directories. If you need a list, the following will do:
Files.walk(Paths.get("/path/to/folder"))
.filter(Files::isRegularFile)
.collect(Collectors.toList())
If you want to return List<File> instead of List<Path> just map it:
List<File> filesInFolder = Files.walk(Paths.get("/path/to/folder"))
.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
You also need to make sure to close the stream! Otherwise you might run into an exception telling you that too many files are open. Read here for more information.
All of the answers on this topic that make use of the new Java 8 functions are neglecting to close the stream. The example in the accepted answer should be:
try (Stream<Path> filePathStream=Files.walk(Paths.get("/home/you/Desktop"))) {
filePathStream.forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
}
});
}
From the javadoc of the Files.walk method:
The returned stream encapsulates one or more DirectoryStreams. If
timely disposal of file system resources is required, the
try-with-resources construct should be used to ensure that the
stream's close method is invoked after the stream operations are completed.
One remark according to get all files in the directory.
The method Files.walk(path) will return all files by walking the file tree rooted at the given started file.
For instance, there is the next file tree:
\---folder
| file1.txt
| file2.txt
|
\---subfolder
file3.txt
file4.txt
Using the java.nio.file.Files.walk(Path):
Files.walk(Paths.get("folder"))
.filter(Files::isRegularFile)
.forEach(System.out::println);
Gives the following result:
folder\file1.txt
folder\file2.txt
folder\subfolder\file3.txt
folder\subfolder\file4.txt
To get all files only in the current directory use the java.nio.file.Files.list(Path):
Files.list(Paths.get("folder"))
.filter(Files::isRegularFile)
.forEach(System.out::println);
Result:
folder\file1.txt
folder\file2.txt
import java.io.File;
public class ReadFilesFromFolder {
public static File folder = new File("C:/Documents and Settings/My Documents/Downloads");
static String temp = "";
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Reading files under the folder "+ folder.getAbsolutePath());
listFilesForFolder(folder);
}
public static void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
// System.out.println("Reading files under the folder "+folder.getAbsolutePath());
listFilesForFolder(fileEntry);
} else {
if (fileEntry.isFile()) {
temp = fileEntry.getName();
if ((temp.substring(temp.lastIndexOf('.') + 1, temp.length()).toLowerCase()).equals("txt"))
System.out.println("File= " + folder.getAbsolutePath()+ "\\" + fileEntry.getName());
}
}
}
}
}
In Java 7 and higher you can use listdir
Path dir = ...;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file: stream) {
System.out.println(file.getFileName());
}
} catch (IOException | DirectoryIteratorException x) {
// IOException can never be thrown by the iteration.
// In this snippet, it can only be thrown by newDirectoryStream.
System.err.println(x);
}
You can also create a filter that can then be passed into the newDirectoryStream method above
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
public boolean accept(Path file) throws IOException {
try {
return (Files.isRegularFile(path));
} catch (IOException x) {
// Failed to determine if it's a file.
System.err.println(x);
return false;
}
}
};
For other filtering examples, [see documentation].(http://docs.oracle.com/javase/tutorial/essential/io/dirs.html#glob)
private static final String ROOT_FILE_PATH="/";
File f=new File(ROOT_FILE_PATH);
File[] allSubFiles=f.listFiles();
for (File file : allSubFiles) {
if(file.isDirectory())
{
System.out.println(file.getAbsolutePath()+" is directory");
//Steps for directory
}
else
{
System.out.println(file.getAbsolutePath()+" is file");
//steps for files
}
}
Just walk through all Files using Files.walkFileTree (Java 7)
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("file: " + file);
return FileVisitResult.CONTINUE;
}
});
If you want more options, you can use this function which aims to populate an arraylist of files present in a folder. Options are : recursivility and pattern to match.
public static ArrayList<File> listFilesForFolder(final File folder,
final boolean recursivity,
final String patternFileFilter) {
// Inputs
boolean filteredFile = false;
// Ouput
final ArrayList<File> output = new ArrayList<File> ();
// Foreach elements
for (final File fileEntry : folder.listFiles()) {
// If this element is a directory, do it recursivly
if (fileEntry.isDirectory()) {
if (recursivity) {
output.addAll(listFilesForFolder(fileEntry, recursivity, patternFileFilter));
}
}
else {
// If there is no pattern, the file is correct
if (patternFileFilter.length() == 0) {
filteredFile = true;
}
// Otherwise we need to filter by pattern
else {
filteredFile = Pattern.matches(patternFileFilter, fileEntry.getName());
}
// If the file has a name which match with the pattern, then add it to the list
if (filteredFile) {
output.add(fileEntry);
}
}
}
return output;
}
Best, Adrien
File directory = new File("/user/folder");
File[] myarray;
myarray=new File[10];
myarray=directory.listFiles();
for (int j = 0; j < myarray.length; j++)
{
File path=myarray[j];
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String s = "";
while (br.ready()) {
s += br.readLine() + "\n";
}
}
nice usage of java.io.FileFilter as seen on https://stackoverflow.com/a/286001/146745
File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
static File mainFolder = new File("Folder");
public static void main(String[] args) {
lf.getFiles(lf.mainFolder);
}
public void getFiles(File f) {
File files[];
if (f.isFile()) {
String name=f.getName();
} else {
files = f.listFiles();
for (int i = 0; i < files.length; i++) {
getFiles(files[i]);
}
}
}
I think this is good way to read all the files in a folder and sub folder's
private static void addfiles (File input,ArrayList<File> files)
{
if(input.isDirectory())
{
ArrayList <File> path = new ArrayList<File>(Arrays.asList(input.listFiles()));
for(int i=0 ; i<path.size();++i)
{
if(path.get(i).isDirectory())
{
addfiles(path.get(i),files);
}
if(path.get(i).isFile())
{
files.add(path.get(i));
}
}
}
if(input.isFile())
{
files.add(input);
}
}
Simple example that works with Java 1.7 to recursively list files in directories specified on the command-line:
import java.io.File;
public class List {
public static void main(String[] args) {
for (String f : args) {
listDir(f);
}
}
private static void listDir(String dir) {
File f = new File(dir);
File[] list = f.listFiles();
if (list == null) {
return;
}
for (File entry : list) {
System.out.println(entry.getName());
if (entry.isDirectory()) {
listDir(entry.getAbsolutePath());
}
}
}
}
While I do agree with Rich, Orian and the rest for using:
final File keysFileFolder = new File(<path>);
File[] fileslist = keysFileFolder.listFiles();
if(fileslist != null)
{
//Do your thing here...
}
for some reason all the examples here uses absolute path (i.e. all the way from root, or, say, drive letter (C:\) for windows..)
I'd like to add that it is possible to use relative path as-well.
So, if you're pwd (current directory/folder) is folder1 and you want to parse folder1/subfolder, you simply write (in the code above instead of ):
final File keysFileFolder = new File("subfolder");
Java 8 Files.walk(..) is good when you are soore it will not throw Avoid Java 8 Files.walk(..) termination cause of ( java.nio.file.AccessDeniedException ) .
Here is a safe solution , not though so elegant as Java 8Files.walk(..) :
int[] count = {0};
try {
Files.walkFileTree(Paths.get(dir.getPath()), new HashSet<FileVisitOption>(Arrays.asList(FileVisitOption.FOLLOW_LINKS)),
Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult visitFile(Path file , BasicFileAttributes attrs) throws IOException {
System.out.printf("Visiting file %s\n", file);
++count[0];
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file , IOException e) throws IOException {
System.err.printf("Visiting failed for %s\n", file);
return FileVisitResult.SKIP_SUBTREE;
}
#Override
public FileVisitResult preVisitDirectory(Path dir , BasicFileAttributes attrs) throws IOException {
System.out.printf("About to visit directory %s\n", dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
void getFiles(){
String dirPath = "E:/folder_name";
File dir = new File(dirPath);
String[] files = dir.list();
if (files.length == 0) {
System.out.println("The directory is empty");
} else {
for (String aFile : files) {
System.out.println(aFile);
}
}
}
package com;
import java.io.File;
/**
*
* #author ?Mukesh
*/
public class ListFiles {
static File mainFolder = new File("D:\\Movies");
public static void main(String[] args)
{
ListFiles lf = new ListFiles();
lf.getFiles(lf.mainFolder);
long fileSize = mainFolder.length();
System.out.println("mainFolder size in bytes is: " + fileSize);
System.out.println("File size in KB is : " + (double)fileSize/1024);
System.out.println("File size in MB is :" + (double)fileSize/(1024*1024));
}
public void getFiles(File f){
File files[];
if(f.isFile())
System.out.println(f.getAbsolutePath());
else{
files = f.listFiles();
for (int i = 0; i < files.length; i++) {
getFiles(files[i]);
}
}
}
}
Just to expand on the accepted answer I store the filenames to an ArrayList (instead of just dumping them to System.out.println) I created a helper class "MyFileUtils" so it could be imported by other projects:
class MyFileUtils {
public static void loadFilesForFolder(final File folder, List<String> fileList){
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
loadFilesForFolder(fileEntry, fileList);
} else {
fileList.add( fileEntry.getParent() + File.separator + fileEntry.getName() );
}
}
}
}
I added the full path to the file name.
You would use it like this:
import MyFileUtils;
List<String> fileList = new ArrayList<String>();
final File folder = new File("/home/you/Desktop");
MyFileUtils.loadFilesForFolder(folder, fileList);
// Dump file list values
for (String fileName : fileList){
System.out.println(fileName);
}
The ArrayList is passed by "value", but the value is used to point to the same ArrayList object living in the JVM Heap. In this way, each recursion call adds filenames to the same ArrayList (we are NOT creating a new ArrayList on each recursive call).
There are many good answers above, here's a different approach: In a maven project, everything you put in the resources folder is copied by default in the target/classes folder. To see what is available at runtime
ClassLoader contextClassLoader =
Thread.currentThread().getContextClassLoader();
URL resource = contextClassLoader.getResource("");
File file = new File(resource.toURI());
File[] files = file.listFiles();
for (File f : files) {
System.out.println(f.getName());
}
Now to get the files from a specific folder, let's say you have a folder called 'res' in your resources folder, just replace:
URL resource = contextClassLoader.getResource("res");
If you want to have access in your com.companyName package then:
contextClassLoader.getResource("com.companyName");
You can put the file path to argument and create a list with all the filepaths and not put it the list manually. Then use a for loop and a reader. Example for txt files:
public static void main(String[] args) throws IOException{
File[] files = new File(args[0].replace("\\", "\\\\")).listFiles(new FilenameFilter() { #Override public boolean accept(File dir, String name) { return name.endsWith(".txt"); } });
ArrayList<String> filedir = new ArrayList<String>();
String FILE_TEST = null;
for (i=0; i<files.length; i++){
filedir.add(files[i].toString());
CSV_FILE_TEST=filedir.get(i)
try(Reader testreader = Files.newBufferedReader(Paths.get(FILE_TEST));
){
//write your stuff
}}}
package com.commandline.folder;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FolderReadingDemo {
public static void main(String[] args) {
String str = args[0];
final File folder = new File(str);
// listFilesForFolder(folder);
listFilesForFolder(str);
}
public static void listFilesForFolder(String str) {
try (Stream<Path> paths = Files.walk(Paths.get(str))) {
paths.filter(Files::isRegularFile).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
}
We can use org.apache.commons.io.FileUtils, use listFiles() mehtod to read all the files in a given folder.
eg:
FileUtils.listFiles(directory, new String[] {"ext1", "ext2"}, true)
This read all the files in the given directory with given extensions, we can pass multiple extensions in the array and read recursively within the folder(true parameter).
public static List<File> files(String dirname) {
if (dirname == null) {
return Collections.emptyList();
}
File dir = new File(dirname);
if (!dir.exists()) {
return Collections.emptyList();
}
if (!dir.isDirectory()) {
return Collections.singletonList(file(dirname));
}
return Arrays.stream(Objects.requireNonNull(dir.listFiles()))
.collect(Collectors.toList());
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class AvoidNullExp {
public static void main(String[] args) {
List<File> fileList =new ArrayList<>();
final File folder = new File("g:/master");
new AvoidNullExp().listFilesForFolder(folder, fileList);
}
public void listFilesForFolder(final File folder,List<File> fileList) {
File[] filesInFolder = folder.listFiles();
if (filesInFolder != null) {
for (final File fileEntry : filesInFolder) {
if (fileEntry.isDirectory()) {
System.out.println("DIR : "+fileEntry.getName());
listFilesForFolder(fileEntry,fileList);
} else {
System.out.println("FILE : "+fileEntry.getName());
fileList.add(fileEntry);
}
}
}
}
}
list down files from Test folder present inside class path
import java.io.File;
import java.io.IOException;
public class Hello {
public static void main(final String[] args) throws IOException {
System.out.println("List down all the files present on the server directory");
File file1 = new File("/prog/FileTest/src/Test");
File[] files = file1.listFiles();
if (null != files) {
for (int fileIntList = 0; fileIntList < files.length; fileIntList++) {
String ss = files[fileIntList].toString();
if (null != ss && ss.length() > 0) {
System.out.println("File: " + (fileIntList + 1) + " :" + ss.substring(ss.lastIndexOf("\\") + 1, ss.length()));
}
}
}
}
}
/**
* Function to read all mp3 files from sdcard and store the details in an
* ArrayList
*/
public ArrayList<HashMap<String, String>> getPlayList()
{
ArrayList<HashMap<String, String>> songsList=new ArrayList<>();
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
HashMap<String, String> song = new HashMap<String, String>();
song.put(
"songTitle",
file.getName().substring(0,
(file.getName().length() - 4)));
song.put("songPath", file.getPath());
// Adding each song to SongList
songsList.add(song);
}
}
// return songs list array
return songsList;
}
/**
* Class to filter files which have a .mp3 extension
* */
class FileExtensionFilter implements FilenameFilter
{
#Override
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
You can filter any textfiles or any other extension ..just replace it with .MP3
This will Read Specified file extension files in given path(looks sub folders also)
public static Map<String,List<File>> getFileNames(String
dirName,Map<String,List<File>> filesContainer,final String fileExt){
String dirPath = dirName;
List<File>files = new ArrayList<>();
Map<String,List<File>> completeFiles = filesContainer;
if(completeFiles == null) {
completeFiles = new HashMap<>();
}
File file = new File(dirName);
FileFilter fileFilter = new FileFilter() {
#Override
public boolean accept(File file) {
boolean acceptFile = false;
if(file.isDirectory()) {
acceptFile = true;
}else if (file.getName().toLowerCase().endsWith(fileExt))
{
acceptFile = true;
}
return acceptFile;
}
};
for(File dirfile : file.listFiles(fileFilter)) {
if(dirfile.isFile() &&
dirfile.getName().toLowerCase().endsWith(fileExt)) {
files.add(dirfile);
}else if(dirfile.isDirectory()) {
if(!files.isEmpty()) {
completeFiles.put(dirPath, files);
}
getFileNames(dirfile.getAbsolutePath(),completeFiles,fileExt);
}
}
if(!files.isEmpty()) {
completeFiles.put(dirPath, files);
}
return completeFiles;
}
This will work fine:
private static void addfiles(File inputValVal, ArrayList<File> files)
{
if(inputVal.isDirectory())
{
ArrayList <File> path = new ArrayList<File>(Arrays.asList(inputVal.listFiles()));
for(int i=0; i<path.size(); ++i)
{
if(path.get(i).isDirectory())
{
addfiles(path.get(i),files);
}
if(path.get(i).isFile())
{
files.add(path.get(i));
}
}
/* Optional : if you need to have the counts of all the folders and files you can create 2 global arrays
and store the results of the above 2 if loops inside these arrays */
}
if(inputVal.isFile())
{
files.add(inputVal);
}
}

Delete 400-layer folder by Java

I'm writing a File Manager for Android ,and I create 400-layer folder when write a copy method , i have fix the bug ,but i can't delete the folders have created by call delete() method.
My delete() method works well when delete normal folders but not work with the 400-layer folder.
my delete() method here
public boolean delete(File file) {
boolean isSuccess = false;
if (file.isDirectory()) {
File[] fileArray = file.listFiles();
for (File tFile : fileArray) {
delete(tFile);
}
file.delete();
} else {
file.delete();
}
return isSuccess;
}
I have fixed it with a if(null) before recursive
public boolean delete(File file) {
boolean isSuccess = false;
if (file.isDirectory()) {
File[] fileArray = file.listFiles();
//change here
if (file != null) {
for (File tFile : fileArray) {
delete(tFile);
}
}
file.delete();
} else {
file.delete();
}
return isSuccess;
}

Finding last folder in list of folders

Is there any faster method to find a folder that has no other folders inside?
File dir = new File("C:\\Users\\axs0552\\Desktop\\barcode\\");
File[] cartella = dir.listFiles();
List<String> Nome_cartela = null;
if (cartella == null) {
logger.debug("ERRORE: cartella inesistente, oppure directoy errata !!");
} else {
for (int i = 0; i < cartella.length; i++) {
if (cartella[i].isDirectory()) {
System.out.println("cartella radice n° :" + i + " " + cartella[i].getName());
File[] figli = cartella[i].listFiles();
for (int j = 0; i < figli.length; i++) {
if (figli[i].isDirectory()) {
System.out.println("cartella figlio n° :" + j + " " + figli[i].getName());
}
}
}
}
}
If you want to recursively examine all directories I suggest using a FileVisitor. This is a simple example that just outputs all names on entering and leaving and counts the directories:
public class MyFileVisitor implements FileVisitor<Path> {
private int dirCount = 0;
#Override
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes bfa) throws IOException {
System.out.println("Entering directory: " + path);
dirCount++;
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult postVisitDirectory(Path path, IOException ex) throws IOException {
System.out.println("Leaving directory: " + path);
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException {
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path path, IOException ex) throws IOException {
return FileVisitResult.CONTINUE;
}
public int getDirCount() {
return dirCount;
}
}
main could look like this:
public class Main {
public static void main(String[] args) {
Path path = Paths.get("c:/users");
MyFileVisitor fileVisitor = new MyFileVisitor();
try {
Files.walkFileTree(path, fileVisitor);
System.out.println(fileVisitor.getDirCount() + " directories");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
If you only want to have the logic from your script you could write it like this (note that findFolderWihtoutSubfolders is static only for simplicity of main):
package tests;
import java.io.File;
public class Directories {
public static File findFolderWithoutSubfolders(File dir) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
boolean flag = true;
for (File ff : f.listFiles()) {
if (ff.isDirectory()) {
flag = false;
break;
}
}
if (flag) {
return f;
}
}
}
return null;
}
public static void main(String[] args) {
File f = findFolderWithoutSubfolders(new File("C:\\Users\\stack\\test"));
if (f != null) {
System.out.println("Folder is : " + f.getName());
} else {
System.out.println("no folder found");
}
}
}
The simple way to print all empty directories below a root directory could be the below snippet.
assuming the follwing structure (file are named *.file)
/tmp/foo
/tmp/foo/bar
/tmp/foo/bar/bar.file
/tmp/foo/bar/barfoo
/tmp/foo/bar/foobar
/tmp/foo/bar/foobar/foobar.file
/tmp/foo/bar.file
/tmp/foo/baz
The snippet
Path rootPath = Paths.get("/tmp/foo");
Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
.map(Path::toFile)
.filter((file) -> file.isDirectory() && file.listFiles().length == 0)
.forEach(System.out::println);
output
/tmp/foo/bar/barfoo
/tmp/foo/baz
the following directories are not printed
/tmp/foo/bar - contains subdirectories and a file
/tmp/foo/bar - contains a file
the method is not recursive. only two levels are checked. tree browsing is achieved using the file tree walking from nio2
import java.io.File;
import java.io.FileFilter;
// yet another file util class
public class YAFU {
public static void main(String[] args) {
File[] simpleFolders = YAFU.simpleFolders(new File("/tmp"));
if (simpleFolders == null)
System.out.println("nothing found");
else
for (File f : simpleFolders) {
System.out.println(f.getName());
}
}
public static boolean containsDirectories(File file) {
if (file == null || !file.isDirectory()) {
return false;
} else {
File[] found = file.listFiles(new FileFilter() {
#Override
public boolean accept(File file) {
return file.isDirectory();
}
});
return (found == null) ? false : found.length > 0;
}
}
public static File[] simpleFolders(File rootDir) {
if (rootDir == null || !rootDir.isDirectory()) {
return null;
} else {
return rootDir.listFiles(new FileFilter() {
#Override
public boolean accept(File file) {
return containsDirectories(file);
}
});
}
}
}
you can do following
public class LastFolderFinder {
public static void main(final String[] args){
final Path dir = Paths.get("C:\\Users\\axs0552\\Desktop\\barcode\\");
visitDir(dir);
}
private static void visitDir(final Path dir) {
try (final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir, new DirectoryFilter());) {
final Iterator<Path> iterator = directoryStream.iterator();
if (iterator.hasNext()) {
while (iterator.hasNext()) {
final Path next = iterator.next();
visitDir(next);
}
} else {
System.out.println("last directory: " + dir);
}
} catch (final Exception exception) {
exception.printStackTrace();
}
}
}
class DirectoryFilter implements Filter<Path> {
#Override
public boolean accept(final Path entry) throws IOException {
return entry.toFile().isDirectory();
}
}
or you can do following updated https://stackoverflow.com/a/36084399/3333885 a little
public class LastFolderFinder {
public static void main(final String[] args) throws IOException {
final Path dir = Paths.get("C:\\Users\\axs0552\\Desktop\\barcode\\");
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
#Override
public FileVisitResult preVisitDirectory(final Path path, final BasicFileAttributes bfa) throws IOException {
if (hasDirectories(path)) {
return FileVisitResult.CONTINUE;
}
System.err.println(path);
return FileVisitResult.SKIP_SUBTREE;
}
#Override
public FileVisitResult postVisitDirectory(final Path path, final IOException ex) throws IOException {
return FileVisitResult.CONTINUE;
}
#Override
public FileVisitResult visitFile(final Path path, final BasicFileAttributes bfa) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
private static boolean hasDirectories(final Path dir) {
try (final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir, new DirectoryFilter());) {
final Iterator<Path> iterator = directoryStream.iterator();
return iterator.hasNext();
} catch (final Exception exception) {
exception.printStackTrace();
}
return false;
}
}
class DirectoryFilter implements Filter<Path> {
#Override
public boolean accept(final Path entry) throws IOException {
return entry.toFile().isDirectory();
}
}

Retrieve all XML file names from a directory using JAVA

I have a directory with multiple files. I need to retrieve only XML file names in a List using Java. How can I accomplish this?
Try this, {FilePath} is directory path:
public static void main(String[] args) {
File folder = new File("{FilePath}");
File[] listOfFiles = folder.listFiles();
for(int i = 0; i < listOfFiles.length; i++){
String filename = listOfFiles[i].getName();
if(filename.endsWith(".xml")||filename.endsWith(".XML")) {
System.out.println(filename);
}
}
}
You can use also a FilenameFilter:
import java.io.File;
import java.io.FilenameFilter;
public class FileDemo implements FilenameFilter {
String str;
// constructor takes string argument
public FileDemo(String ext) {
str = "." + ext;
}
// main method
public static void main(String[] args) {
File f = null;
String[] paths;
try {
// create new file
f = new File("c:/test");
// create new filter
FilenameFilter filter = new FileDemo("xml");
// array of files and directory
paths = f.list(filter);
// for each name in the path array
for (String path : paths) {
// prints filename and directory name
System.out.println(path);
}
} catch (Exception e) {
// if any error occurs
e.printStackTrace();
}
}
#Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(str.toLowerCase());
}
}
You can filter by using File.filter(FileNameFilter). Provide your implementation for FileNameFilter
File f = new File("C:\\");
if (f.isDirectory()){
FilenameFilter filter = new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
if(name.endsWith(".xml")){
return true;
}
return false;
}
};
if (f.list(filter).length > 0){
/* Do Something */
}
}

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