recursively delete a folder in java - java

I already have a code that works, but I don't want it to actually delete the temp folder if possible. I am using the apache fileutils. Also does anyone know how to exclude folders from being deleted?
public class Cleartemp {
static String userprofile = System.getenv("USERPROFILE");
public static void main(String[] args) {
try {
File directory = new File(userprofile+"\\AppData\\Local\\Temp");
//
// Deletes a directory recursively. When deletion process is fail an
// IOException is thrown and that's why we catch the exception.
//
FileUtils.deleteDirectory(directory);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Here's an actually recursive method:
public void deleteDirectory(File startFile, FileFilter ignoreFilter) {
if(startFile.isDirectory())
for(File f : startFile.listFiles()) {
deleteDirectory(f, ignoreFilter);
}
if(!ignoreFilter.accept(startFile)) {
startFile.delete();
}
}
Hand it a file filter set to return true for directories (see below) to make it not delete directories. You can also add exceptions for other files too
FileFilter folderFilter = new FileFilter() {
#Override
public boolean accept(File paramFile) {
return paramFile.isDirectory();
}
};

How about FileUtils.cleanDirectory ? It cleans a directory without deleting it.
You could also use Apache Commons DirectoryWalker if you need some filtering logic. One of the examples on the page includes FileCleaner implementation.

Simple,
Use isDirectory() to exclude it from being deleted.
Refer here: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#isDirectory()

First ever post, don't consider myself an expert but am stuck with 1.4...
Here's a recursive delete method that works well, deletes all files and subfolders within a parent folder then the parent folder itself, assumes the File being passed is a directory as it is in my case.
private void deleteTemp(File tempDir) {
File[] a = (tempDir.listFiles());
for (int i = 0; i < a.length; i++) {
File b = a[i];
if (b.isDirectory())
deleteTemp(b);
b.delete();
}
tempDir.delete();
}

Related

Vaadin - deleting temporary Files from Upload-Component

hope you can help me. I have a Spring-Boot vaadin-Project with a few Upload-Fields.
Everythings fine. if you click on the send button in the end everything is processed and tempfiles are deleted. Though when you upload a file and leave the site then the temp-directory stays untouched. Is there any way to programatically delete all temporary files when a new instance is called?
When I upload a file on a built-with-vaadin-website and leave the site then, my temp directory gets fuller and fuller. i just want to delete all files which were created in the actual Vaadin Session when starting a new one. Or is there a way to find all files created in a spring session periodically?
I would create a custom VaadinServiceInitListener class (annotated with #Component), I would make a deleting method and in the serviceInit method I would call the deleting method with the uploading path. Something like this:
#Component
public class ApplicationServiceInitListener
implements VaadinServiceInitListener {
#Override
public void serviceInit(ServiceInitEvent event) {
// Delete the upload directory's content
try {
deleteDirectory(new File("[your_upload_directory_path]"));
} catch (IOException e) {
throw new RuntimeException(e);
}
// ...
}
private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
}
Service Init Listener Vaadin doc: https://vaadin.com/docs/latest/advanced/service-init-listener
Ps: Of course, you can also use a File Util class, e.g. from common-io.

Java exception in the thread error

I'm trying to create a program that will go to specific director(ies) and print files that are located in there.
Here's the code:
import java.io.File;
public class FileViewer {
mport java.io.File;
public class FileViewer {
public void Srch(String path)
{
File root=new File(path);
File[] list=root.listFiles();
for(File x:list)
{
if(x.isDirectory())
{
Srch(x.getAbsolutePath());
System.out.println(x.getAbsolutePath());
}
else
{
System.out.println(x.getAbsolutePath());
}}}}
public class Viewer {
public static void main(String[] args) {
FileViewer Srch2=new FileViewer();
Srch2.Srch("Home\\Documents");
}
}
And I'm getting following error
Exception in thread "main" java.lang.NullPointerException
at FileViewer.Srch(FileViewer.java:9)
at Viewer.main(Viewer.java:7)
What seems to be the problem? Thanks
File.listFiles() may return null if it's used on an object is not a directory, or can't be accessed. I'm not sure whether "Home\\Documents" exists, but you should probably take a defensive approach in your method:
public void Srch(String path) {
File root=new File(path);
if (root.exist() && root.isDirectory()) {
File[] list=root.listFiles();
for(File x:list) {
Srch(x.getAbsolutePath());
System.out.println(x.getAbsolutePath());
}
} else {
System.out.println(x.getAbsolutePath());
}
}
The problem is most occurring because in your Srch method you call listFiles before checking whether the root is actually a directory. As stated in the javadoc of that method
Returns null if this abstract pathname does not denote a directory
Just make sure you handle that case as well. For example
public class FileViewer {
public void Srch(String path){
File root=new File(path);
File[] list=root.listFiles();
if ( list == null ){
return;
}
for(File x:list){
if(x.isDirectory()){
Srch(x.getAbsolutePath());
System.out.println(x.getAbsolutePath());
} else {
System.out.println(x.getAbsolutePath());
}
}
}
}
Note that a NullPointerException is one of the easiest exceptions to debug in Java. Just place a breakpoint with your debugger at the line indicated in the stacktrace and check which object is null. Then you know why you get the exception and you can start fixing it. Typically this boils down to:
null is a valid situation and you forgot to handle it
null was not a valid situation, and then you need to investigate how that variable could be null and fix it upstream
Sidenote for the next time you post a question:
Provide proper indentation for the code you post. Yours is really hard to read
Try to avoid copy-paste errors when pasting code. The first two lines are duplicated
You posted the stacktrace (good), but it would even be better if you indicated to which line the line number appearing in the stacktrace corresponds. Remember, we do not have access to your code so linking a line number from a stacktrace to the code snippet you post is far from trivial
A java.lang.NullPointerException appears, when Java wants to use something that isn't there. Could it be that the path is wrong?
Here :
File[] list=root.listFiles();
The method listFile may return "null".
Check for the Javadoc: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#listFiles()
So then you do a foreach on this null variable, you get a NullPointerException if the pathname given to Srch method does not denote a directory.
if(list!=null)
{
for(File x:list)
{
if(x.isDirectory())
{
Srch(x.getAbsolutePath());
System.out.println(x.getAbsolutePath());
}
else
{
System.out.println(x.getAbsolutePath());
}
}
}
May be "Home\Documents" not exists, try with abslotute path.

Creating File & Directories not working properly

I am currently working on a method that will create files and directories. Bellow is the use case & problem explained.
1) When a user specifies a path e.g "/parent/sub folder/file.txt", the system should be able to create the directory along with the file.txt. (This one works)
2) When a user specifies a path e.g "/parent/sub-folder/" or "/parent/sub-folder", the system should be able to create all directories. (Does not work), Instead of it creating the "/sub-folder/" or /sub-folder" as a folder, it will create a file named "sub-folder".
Here is the code I have
Path path = Paths.get(rootDir+"test/hello/");
try {
Files.createDirectories(path.getParent());
if (!Files.isDirectory(path)) {
Files.createFile(path);
} else {
Files.createDirectory(path);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
You need to use createDirectories(Path) instead of createDirectory(path). As explained in the tutorial:
To create a directory several levels deep when one or more of the
parent directories might not yet exist, you can use the convenience
method, createDirectories(Path, FileAttribute). As with the
createDirectory(Path, FileAttribute) method, you can specify an
optional set of initial file attributes. The following code snippet
uses default attributes:
Files.createDirectories(Paths.get("foo/bar/test"));
The directories
are created, as needed, from the top down. In the foo/bar/test
example, if the foo directory does not exist, it is created. Next, the
bar directory is created, if needed, and, finally, the test directory
is created.
It is possible for this method to fail after creating some, but not
all, of the parent directories.
Not sure of which File API you are using. But find below the simplest code to create file along with folders using java.io package.
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
FileTest fileTest = new FileTest();
fileTest.createFile("C:"+File.separator+"folder"+File.separator+"file.txt");
}
public void createFile(String rootDir) {
String filePath = rootDir;
try {
if(rootDir.contains(File.separator)){
filePath = rootDir.substring(0, rootDir.lastIndexOf(File.separator));
}
File file = new File(filePath);
if(!file.exists()) {
System.out.println(file.mkdirs());
file = new File(rootDir);
System.out.println(file.createNewFile());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}

How to get files from a directory on specific condition one file must come after another

Hi I am trying to load data into table using data files. I am using JDBC batch upload. After I load data from test.data into table, I want to validate it using expected-table.data. So in the following method first when test.data comes I want to do batch upload and then it should validate data using expected file but the following code does not work as expeted-data files comes in first iteration and test.data in second iteration. Please help I am new to file programming. Thanks in advance.
public static void loadFromFilesNValidateTable(Schema schema, final File folder)
{
for (final File fileEntry : folder.listFiles())
{
if (fileEntry.isDirectory())
{
loadFromFilesNValidateTable(schema,fileEntry);
}
else
{
if(fileEntry.getName().equals("test.data"))
{
BatchUpload.batchUpload(schema,fileEntry.getAbsolutePath());
}
if(fileEntry.getName().equals("expected-table.data"))
{
validateData(schema,fileEntry.getAbsolutePath());
}
}
}
}
Use a FileFilter
public static void loadFromFilesNValidateTable(TableDef schema, final File folder) {
// Process folders recursively
for(final File subFolder : folder.listFiles(new DirectoryFilter())){
loadFromFilesNValidateTable(schema, subFolder);
}
// Process data files
for (final File dataFileEntry : folder.listFiles(new FileNameFilter("test.data"))) {
BatchUpload.batchUpload(schema,dataFileEntry.getAbsolutePath());
}
// Process expected files
for (final File expectedFileEntry : folder.listFiles(new FileNameFilter("expected-table.data"))) {
validateData(schema,expectedFileEntry.getAbsolutePath());
}
}
public class FileNameFilter implements FileFilter {
private String name;
public FileNameFilter(String name){
this.name = name;
}
public boolean accept(File pathname){
return pathname.getName().equals(name)
}
}
public class DirectoryFilter implements FileFilter {
public boolean accept(File pathname){
return pathname.isDirectory();
}
}
Note: Apache commons-io provides a lot of FileFilters ready to use http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/filefilter/package-summary.html
I have 2 things to suggest you:
1. Change equals method with matches method. Matches method is the best method to compare
Strings.
2. Try to separate the moment in which you do batchUpload from that in which you do
validateData. Maybe you jump some files. For example the algorithm finds first the
"expected-table.data" file and then the "test.data" file. In this case it doesn't validate
the file. I hope my answer is usefull and I hope I have understood your problem : )
You should change the algorithm to something like this:
for (each directory in current directory) {
loadFromFilesNValidateTable(schema, directory);
}
if (current directory contains file "test.data") {
batchUpload();
if (current directory contains file "expected-table.data") {
validateData();
}
}

Eliminate hardcoded file paths and urls in web development

I'm currently developing a web site using servlets & spring framework. As usual it contains lots of files (jsp, js, css, images, various resources etc).
I'm trying to avoid writing any hardcoded path, or domain in any file ...
For example as you may know when a request is handled you 'forward' it to a jsp page (it's path probably will be hardcoded). Other examples are imports images/css/js etc in jsp files ...
Is there any general way (or tools) to avoid hardcoded paths/urls so any refactorings won't cause troubles?
EDIT
I use netbeans 7.1.2 ... Unfortunately netbeans only helps with pure java code. When working with jsp files things are limited, and if you add custom tag files and Jsp 2.0 EL is like programming in console mode :p
In the JSP files themselves, you can avoid nearly all hardcoded domain / urls by using JSTL
For example, when creating a link to another page, you would do it like this:
Refer an Entrepreneur!
This means that, regardless of where your webapp is, the link will always have the right url. For example, in my development box this link would be:
http://localhost:8080/accounts/referrals/send.html
But on my production server, it resolves correctly to:
http://wwww.mydomain.com/referrals/send.html
You can see that in my dev server, the webapp context is under /accounts, but on the production machine, it's just under / as the webapp is under the root context.
You can read a small tutorial here
Properties file is always a good option so that you have to make changes if any only at one point.
If you are referencing any static contents (js, images, css, etc), you don't have to hardcode the entire file path. Instead, you can do this:-
<img src="${pageContext.request.contextPath}/resources/images/test.jpg"/>
The rest of the file paths (Hibernate domain mappings, forwarded page in Spring controller, etc) should be relative to your project structure, and most IDEs are smart enough to refactor them without problem... or at least in my case, IntelliJ seems to handle of all that for me.
At some point of time, you need to ask yourself, how much of hardcoding is acceptable vs not acceptable? Further, I wouldn't try to stray too far away from the Spring/Hibernate recommended solutions. If you make everything too abstract, you have a different set of problem to deal with and it becomes counterproductive to other peers that may be inheriting your project in the future.
Actually I just came up with an idea. Since netbeans does analysis and shows dependencies on java code, maybe it's better to handle all paths & domains as java variables.
I've created a package on my project named FileResolver and inside I have one class for each file type on my project (eg one class for Jsp files, one for Css files etc). Inside those files I'll record & hardcode all paths of all files in public static final String variables. Sample:
public class Jsps {
public class layouts{
public static final String main = "layouts/main.jsp";
}
public class pages{
public static final String error = "pages/error.jsp";
public static final String login = "pages/login.jsp";
public static final String register = "pages/register.jsp";
}
...
}
All over my project I should use the variables instead of paths. Then anytime I refactor a file, I'll have only one file to change is the mapping value in those variables ...
And if somethime I need to change the variable, netbeans will refactor all of them in the project at once ...
I think this will work just fine since I keep my project clean from file paths and the only thing I have to worry about is the mapping in that file of the variables to appropriate file paths.
EDIT
I'll write a simple parser to create those java files instead of writting by hand for all files ... I'll update when I finish it
UPDATE
Here is my FileResolverGenerator
public class FileResolverGenerator {
private static final String newFilePath = "C:/Users/Foo/Desktop/Jsps.java";
private static final String scanRootFolder = "C:/Users/Foo/Desktop/myProject/web/WEB-INF/jsp";
private static final String varValueReplaceSource = "C:/Users/Foo/Desktop/myProject/web/WEB-INF/jsp/";
private static final String varValueReplaceTarget = "";
private static final boolean valueAlign = true;
private static final int varNameSpaces = 15;
public static void main(String[] args){
try {
// Create file and a writer
File f = new File(newFilePath);
f.createNewFile();
bw = new BufferedWriter( new FileWriter(f) );
// Execute
filesParser( new File(scanRootFolder) );
// 'Burn' file
bw.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ResolverGenerator.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ResolverGenerator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// ================================================================================================ //
// ============================================= WORK ============================================= //
// ================================================================================================ //
private static void filesParser(File rootFolder) throws FileNotFoundException, IOException{
folderIn(rootFolder);
// Files first
if(!rootFolder.exists()) throw new FileNotFoundException();
for(File f : rootFolder.listFiles()){
if(f==null){ return; }
if(f.isDirectory()){ continue; }
else if(f.isFile()){ writeFileVariable(f); }
}
// Folders next
for(File f : rootFolder.listFiles()){
if(f==null){ return; }
if(f.isDirectory()){ filesParser(f); }
else if(f.isFile()){ continue; }
}
folderOut(rootFolder);
}
// ================================================================================================ //
// ============================================ PRINTS ============================================ //
// ================================================================================================ //
private static BufferedWriter bw;
private static int tabCount = 0;
private static void folderIn(File f) throws IOException{
bw.append("\n\n");
for(int i=0; i<tabCount; i++)
bw.append("\t");
bw.append("public class "+f.getName()+"{\n");
tabCount++;
}
private static void folderOut(File f) throws IOException{
tabCount--;
for(int i=0; i<tabCount; i++)
bw.append("\t");
bw.append("}\n");
}
private static void writeFileVariable(File f) throws IOException{
String varName = f.getName().split("\\.")[0].replaceAll("-", "");
String varValue = f.getPath().replaceAll("\\\\","/")
.replace(varValueReplaceSource.replaceAll("\\\\","/"),varValueReplaceTarget.replaceAll("\\\\","/"));
for(int i=0; i<tabCount; i++)
bw.append("\t");
bw.append("public static final String "+varName+" = ");
if(valueAlign){
for(int i=0; i<varNameSpaces-varName.length(); i++) bw.append(" ");
bw.append("\t"); }
bw.append("\""+varValue+"\";\n");
}
}
Just to be specific ... This scans all files under "/WEB-INF/jsp/" and creates a java file having all jsp files 'registered' to public static final String variables with each path ... The idea is to use the generated java file as reference for all jsps are in project ... always use these variables instead of hardcoded paths ..
This has nothing to do with the project or any project. It's just a tool which saves you
time, instead of doing this by hand for every file in the project.
I also created another class ResolverConsistencyChecker, which takes all variables and checks if the filepath is correct (file exists) ... since we didn't made any changes to filenames and filepaths all tests are passed.
This method should run when testing project for 'errors'
public class ResolverConsistencyChecker {
private static Class checkClass = Jsps.class;
private static String fullPathPrefix = "C:/Users/Foo/Desktop/myProject/web/WEB-INF/jsp/";
public static void main(String[] args){
try {
filesChecker( checkClass );
System.out.println( "Tests passed. All files locations are valid" );
} catch (FileNotFoundException ex) {
Logger.getLogger(ResolverConsistencyChecker.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ResolverConsistencyChecker.class.getName()).log(Level.SEVERE, null, ex);
}
}
// ================================================================================================ //
// ============================================= WORK ============================================= //
// ================================================================================================ //
private static void filesChecker(Class rootClass) throws FileNotFoundException, IOException{
// Check file paths in current class depth
for(Field f : rootClass.getFields()){
try {
String fullFilePath = fullPathPrefix+f.get(f.getName()).toString();
File file = new File( fullFilePath );
if( !file.exists() )
throw new FileNotFoundException("Variable: '"+f.getName()+"'\nFile "+fullFilePath);
} catch (IllegalArgumentException ex) {
Logger.getLogger(ResolverConsistencyChecker.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ResolverConsistencyChecker.class.getName()).log(Level.SEVERE, null, ex);
}
}
// Check for embedded classes
for(Class c : rootClass.getClasses()){
filesChecker(c);
}
}
}

Categories