Read Internal File Name

Asked

Viewed 392 times

4

I need to read the name of the files contained within a package, for example, read the name file01.txt, as in the image below :

pacote com arquivos

So I can go through the package arquivos without having to name a file for reading.

  • Related (if not duplicated): http://answall.com/q/159121/3084

4 answers

6

Can do with listFiles or Files walk. java 8.

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

class Teste {
    public static void main(String[] args) {
        final File pasta = new File("C:\\teste");
        listaArquivos(pasta);   
        listaArquivosJava8(pasta);
    }

    public static void listaArquivos(final File pasta) {
        for (final File fileEntry : pasta.listFiles()) {
            if (! fileEntry.isDirectory()) {
                System.out.println(fileEntry.getName());
            }
        }
    }

    public static void listaArquivosJava8(final File pasta) {
        try(Stream<Path> paths = Files.walk(Paths.get(pasta.toURI()))) {
            paths.forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    System.out.println(filePath);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

Source: Read all files in a Folder - Stackoverflow

  • I can barely see your moves! I was about to edit with a version of java8 :p

  • And I could barely see the movements of you and who else posted the listFiles() :P but I was already writing with this option from the beginning

  • It was worth the brother force, but, the files are "within the project", absolute path does not roll.

  • @ulk' I need to do a test here, but it should be easy to solve, see: http://answall.com/q/15574/3117

  • All right, Math, I’m waiting, buddy.

4

Try the following:

String path = getClass().getResource("arquivos").getPath();

File[] files = new File(path).listFiles();

for(File file : files){
  System.out.println(file.getName());
}

Recalling that the path will only be recovered correctly as indicated in the code, taking into account that the folder arquivos is part of the project. If it is external, you need to inform the absolute path, as in Reply from @Joe Torres

  • Save Diego, then beast, the structure of the project is as in the image, would have to reference it in Build Path also ?

  • @ulk' no, just use that form of the same answer. The getClass() recovers the current class and the getResource() recovers the path to the directory you have entered, in case arquivos, and the getPath() takes the absolute path of this folder. This code has been tested using a structure similar to yours, can test that it will work.

  • I tested in this structure and the getClass().getResource("arquivos").getPath() returned me null, even the folder arquivos being a package of the project.

  • Try to put a bar: getClass().getResource("/arquivos").getPath() if the IDE misses syntax, put two. the file folder in your project is in a hierarchy other than your java class.

3

Look buddy, I’m a little rusty in java, but as far as I can remember, you can list the arches that way:

File folder = new File("c:\pathproprojeto\src\arquivos");
File[] listOfFiles = folder.listFiles();
  • Joe, it was worth the force beast, but by absolute way it does not roll.

1

public static void getImgs(String path){
File file = new File(path);
File[] arquivos = file.listFiles();

for (File arquivo : arquivos) {
    System.out.println(arquivo);
}
}

Browser other questions tagged

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