Catch information from multiple files in a directory

1

I'm doing a Java application that takes information from several *.tif files in a directory, but I can not get information from more than one at a time and need to put the specific path of each file. I'm using Paths for this. If anyone can help me thank you.

Path path = Paths.get("C:\teste2");
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.lastModifiedTime();

long tempo;
tempo = creationTime.toMillis();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(tempo));
    
asked by anonymous 29.06.2016 / 14:07

2 answers

4

Use Files.Walk and a loop repetition. See:

Files.walk(Paths.get("C:\teste2")).forEach(path -> {
    if (Files.isRegularFile(filePath)) {
        BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
        FileTime creationTime = attributes.lastModifiedTime();

        long tempo = creationTime.toMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        System.out.println(sdf.format(tempo));
    }
});
    
29.06.2016 / 14:22
1

You can try a workaround:

File folder = new File("caminho-da-sua-pasta"); //crie uma pasta
File[] listOfFiles = folder.listFiles(); //crie uma lista de arquivos 

//Vamos passar num loop para analisar cada arquivo separadamente
for (File file : listOfFiles) {
    if (file.isFile()) {
        //então é arquivo, não uma subpasta
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); //cria formatador
        System.out.println(sdf.format(file.lastModified())); //aqui voce tem a data formatada do arquivo
    }
}

Note that in this workaround, you are not predicting the existence of subfolders within the path you passed as a parameter.

Hope this helps. Any questions are available.

    
29.06.2016 / 14:28