Get information from multiple files in one directory

Asked

Viewed 188 times

1

I’m making a Java application that takes information from multiple files *.tif in a directory, but I can’t take information from more than one at a time and need to put the specific path of each file. I’m using Paths for that. 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));

2 answers

4


Use Files.Walk and a repeat loop. 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));
    }
});
  • I gave this part, but I could not compare the names of the files of two different directories. You have some idea. I did the comparison like this: if (filePath.getFileName(). equals(f. getName()))

  • Young man, this is not part of the initial doubt. If "this part" has worked, keep the question as it is and open a new one with your other doubt.

1

You can try an alternative solution:

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 solution, you are not predicting the existence of subfolders within the path you passed as parameter.

I hope this helps. Any doubt I’m available.

  • Thank you so much for your help.

  • @R.Santos, try to put a crack sign (`) that you can copy and paste the code here. Here I help you.

  • if (filePath.getFileName().equals(f.getName())). The whole code it has error because it exceeds the maximum character capacity

  • "f. getName()" is the files in the other directory.

  • I’m not identifying the filePath object, what it is, and what you’re trying to do?

  • I am trying to compare files that have the same name regardless of the format of the files and if the names are equal I take the dates of modifications of them.

  • I was able to compare them even in different formats, I used the release, as they will always be the same file formats `String arqA = filePath.getFileName(). toString(); String arqB = f.getName(). toString(); if (arqA.equals(arqB.replace(". txt", ". tif")))

  • That’s it. I was already thinking of another approach, but already you got it. Congratulations.

  • Thanks for the help

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.