2013-01-18 81 views
1

嗨,现在我有以下的方法,我用在一个相同的目录类的时刻,有这个方法来读取一个文件:通过Java中的目录遍历

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; 
} 

我想知道我怎样才能让它变成这样,而不是以“fileName”作为参数,而是可以将目录名称作为参数,然后遍历该目录中的所有文件并对其执行相同的操作。谢谢!

+0

什么会使返回类型?每个文件的'region'也是一样的吗? –

+0

我已经完成文件操作已经有一段时间了,但我相信有一种方法可以设置您正在使用的filePath(而不是默认目录)。那么这只是一个遍历该地点的所有事情的问题。我会查看是否可以找到有用的javadocs – turbo

+0

Region对象每次都是相同的。 –

回答

2

如果您使用的是Java 7,那么您需要看看NIO.2

具体来说,看看Listing a Directory's Contents部分。

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

下面是一个简单的例子,可以帮助:

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; 
} 

你问不正是对因为它是包裹你的老方法,而不是重新写它,但我觉得它有点清洁这方式,并让您选择仍然处理单个文件。请务必根据您的实际需求调整退货类型以及如何处理region(难以从问题中得知)。

0

我们也可以使用递归来处理一个包含子目录的目录。这里我正在逐个删除文件,您可以调用任何其他函数来处理它。

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()); 
} 
0

这是相当简单的,使用它会返回在指定的文件中的文件,它必须是一个目录的列表File#listFiles()。要确保文件是目录,只需使用File#isDirectory()即可。问题出现在您决定如何返回字节缓冲区的位置。由于该方法返回一个2d缓冲区,因此必须使用3d字节的缓冲区数组,或者在这种情况下,List对我来说似乎是最好的选择,因为在目录中存在未知数量的文件。



    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; 
    }