1

Hi right now I have the following method I am using to read one file at a time in a the same directory as the class that has this method:

private byte[][] getDoubleByteArrayOfFile(String fileName, Region region)
    throws IOException
{
    BufferedImage image = ImageIO.read(getClass().getResource(fileName));
    byte[][] alphaInputData =
        new byte[region.getInputXAxisLength()][region.getInputYAxisLength()];
    for (int x = 0; x < alphaInputData.length; x++)
    {
        for (int y = 0; y < alphaInputData[x].length; y++)
        {
            int color = image.getRGB(x, y);
            alphaInputData[x][y] = (byte)(color >> 23);
        }
    }
    return alphaInputData;
}

I was wondering how I can make it so that instead of having "fileName" as a argument I can but a directory name as a argument and then iterate through all of the files within that directory and perform the same operation on it. Thanks!

3
  • What would that make the return type? Also would region be the same for each file? Jan 18, 2013 at 20:26
  • It's been a while since I've done file operations, but I believe there is a way to set the filePath that you are working in (instead of the default directory). Then it would just be a matter of iterating through everything at that location. I'll look to see if I can find helpful javadocs
    – turbo
    Jan 18, 2013 at 20:28
  • The Region object would be the same each time.
    – letter Q
    Jan 18, 2013 at 20:56

5 Answers 5

2

If you are using Java 7, then you need to take a look at NIO.2.

Specifically, take a look at the Listing a Directory's Contents section.

Path dir = Paths.get("/directory/path");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
    for (Path file: stream) {
        getDoubleByteArrayOfFile(file.getFileName(), someRegion);
    }
} 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);
}
1

Here is a quick example that may help:

private ArrayList<byte[][]> getDoubleByteArrayOfDirectory(String dirName,
    Region region) throws IOException {
       ArrayList<byte[][]> results = new ArrayList<byte[][]>();
       File directory = new File(dirName);
       if (!directory.isDirectory()) return null //or handle however you wish
       for (File file : directory.listFiles()) {
           results.add(getDoubleByteArrayOfFile(file.getName()), region);
       }  
       return results;
}

Not exactly what you asked for since it's wrapping your old method rather than re-writing it, but I find it a bit cleaner this way, and leaves you with the option of still processing a single file. Be sure to tweak the return type and how to handle the region based on your actual requirements (hard to tell from the question).

1

It is rather simple, using the File#listFiles() which returns a list of files in the specified File, which must be a directory. To make sure that the File is a directory, simply use File#isDirectory(). The problem occurs where you decide how to return the byte buffer. Since the method returns a 2d buffer, it is necessary to use a 3d byte buffer array, or in this case a List seems to me like the best choice since an unknown number of files will exist in the directory in question.



    private List getDoubleByteArrayOfDirectory(String directory, Region region) throws IOException {
      File directoryFile = new File(directory);

      if(!directoryFile.isDirectory()) {
        throw new IllegalArgumentException("path must be a directory");
      }

      List results = new ArrayList();

      for(File temp : directoryFile.listFiles()) {
        if(temp.isDirectory()) {
          results.addAll(getDoubleByteArrayOfDirectory(temp.getPath(), region));
        }else {
          results.add(getDoubleByteArrayOfFile(temp.getPath(), region));
        }
      }
      return results;
    }

0

You can, see the list and listFiles documentation for how to do this.

0

We can use recursion to process a directory with subdirectories also. Here I am deleting file one by one, you can call any other function to process it.

public static void recursiveProcess(File file) {
    //to end the recursive loop
    if (!file.exists())
        return;

    //if directory, go inside and call recursively
    if (file.isDirectory()) {
        for (File f : file.listFiles()) {
            //call recursively
            recursiveProcess(f);
        }
    }
    //call processing function, for example here I am deleting
    file.delete();
    System.out.println("Deleted (Processed) file/folder: "+file.getAbsolutePath());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.