-2

In my application I want to create a folder (named batches) if it is not exists in the system.If exists return the reference object of the file.I dont know the path of the file.

File f=new File("batches");
if(!f.exists())
{
  f.mkdir();
}

This create a directory in root folder and return this directory in second execution.

User123
  • 71
  • 3
  • 14

3 Answers3

0

You can check all connected drives: Find all drive letters in Java

Then iterate over all directories (and subdirectories) How do I iterate through the files in a directory in Java?

This will however be a very lengthy process. If we know the purpose of your program we could help you with a more elegant solution.

Community
  • 1
  • 1
JohannisK
  • 535
  • 2
  • 10
  • In that case you'll have to loop over everything. I would scan images and folder in the same loop, and build references to all images, then at the end of the loop you'll know if the folder exists, create a new one if necesary, and copy all found images (instead of looping over everything twice). I would also look for a way to exclude directories you don't want scanned. – JohannisK May 11 '15 at 09:02
0

The below code creates the directory batches first time the code is executed,next time it print "Already present" stating directory is already created.

If you have already executed the app once the directory is created outside the src

File f=new File("batches");
      if(!f.exists())
      {
        f.mkdir();
      }
      else{
          System.out.println("Already present!");
      }
brijesh
  • 760
  • 6
  • 4
0

Maybe something like this will do:

import java.io.*; 
import java.nio.file.*; 
import java.nio.file.attribute.*; 
import static java.nio.file.FileVisitResult.*; 

public class GetBatchesDir {     
    public static class Finder extends SimpleFileVisitor<Path> {
        private final PathMatcher matcher;
        public Path pathFound=null;

        Finder(String pattern) {
            matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
        }

        boolean found(Path file) {
            Path name = file.getFileName();
            return (name != null && matcher.matches(name));
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) {
            if (found(dir)) {
                pathFound=dir;
                return TERMINATE;
            }
            else
                return CONTINUE;
        }
    }

    public static void main(String[] args) throws IOException {
        Finder finder = new Finder("batches");
        Files.walkFileTree(Paths.get("."), finder);
        PrintWriter out;
        if (finder.pathFound != null)
            out=new PrintWriter(finder.pathFound.toString()+"/file.txt");
        else { 
            new File("./batches").mkdir();
            out=new PrintWriter("./batches/file.txt");
        }
        out.println("Hello");
        out.close();
    }
}

NIO is generally too ambitious for this but has neat finder functionality, so we use it for that, and simple File for the rest. I have used "." as the root of the filesystem, you can probably find something more suitable. You wil also have to figure out what to do if the file exist, disk is full and so on.

HWJ
  • 77
  • 5