0

I'm trying to get the names of the files that exist in a directory, using InputStream, but it always returns empty: []

Can anyone help me with the code.

The Project have the structure:

enter image description here

The code that i made:

        // Gets the info.
        String path = "/imagens/";
        InputStream is = getClass().getResourceAsStream(path);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        br.lines()
          .map(l -> path + "/" + l)
          .collect(toCollection(FXCollections::observableArrayList));
        // Shows the info.
        URL resource = getClass().getResource("/imagens/");
        try(InputStream is2 = resource.openStream())
        {
          System.out.println(observableArrayList());
        }
        catch (IOException ex)
        {
          Logger.getLogger(JavaFX_Paths.class.getName()).log(Level.SEVERE, null, ex);
        }

New code:

HBox root = new HBox();
    Button btn = null;
    ArrayList<String> items = new ArrayList<String>();
        String textLine;
        try
    {
            BufferedReader br = new BufferedReader(new FileReader("src/imagens/lista_icones.txt"));
            while ((textLine = br.readLine()) != null)
                items.add(textLine);
            br.close();
    }
    catch (IOException ex)
    {
      Logger.getLogger(JavaFX_Paths.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (int i = 0; i < items.size(); i++)
    {
      Image img = new Image(getClass().getResourceAsStream("/imagens/" + items.get(i) + ".png"));
      btn = new Button();
      btn.setId(items.get(i));
      btn.setGraphic(new ImageView(img));
      root.getChildren().addAll(btn);
    }

FINAL CODE (and finally working :)))) I used InputStream, InputStreamReader and BufferedReader...nothing to do with File objects or FileReaders.

HBox root = new HBox();
    ArrayList<String> items = new ArrayList<String>();
        String textLine;
    Button btn = null;
        try
    {
      InputStream inputStream = JavaFX_Paths.class.getResourceAsStream("/imagens/lista_imagens.txt");
      InputStreamReader inputReader = new InputStreamReader(inputStream);
            BufferedReader bufferReader = new BufferedReader(inputReader);
            while ((textLine = bufferReader.readLine()) != null)
                items.add(textLine);
            bufferReader.close();
      inputReader.close();
      inputStream.close();
    }
    catch (IOException ex)
    {
      Logger.getLogger(JavaFX_Paths.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (int i = 0; i < items.size(); i++)
    {
      Image img = new Image(getClass().getResourceAsStream("/imagens/" + items.get(i) + ".png"));
      btn = new Button();
      btn.setId(items.get(i));
      btn.setGraphic(new ImageView(img));
      root.getChildren().addAll(btn);
    }
Pedro Figueiredo
  • 457
  • 5
  • 13
  • It doesn't really make sense to try to do this. When you deploy your application, the images will be part of the jar file that is created to contain the application: they will not be files at all. Either just keep a list of the images in a text or properties file, or perhaps bundle them into a single zip file that you can extract them from. – James_D Aug 07 '17 at 21:25
  • But this app that i'm creating has 217 images, i think myself is a bit weird to create a pre-list of 217 names. I tried with object File, and it works when i tested the app in Netbeans but fails when i try to run the JAR file. So, i was trying to get the names of the image files directly from the directory and then use those names to show the images in the app. I read that we can try to use InputStream to read the files that the JAR file constains. – Pedro Figueiredo Aug 08 '17 at 16:06
  • Yes; there's no general way to get a list of everything in a "folder" for a general class loader. Some frameworks (e.g. Spring) implement "classpath scanning": you could look at how those work, but I think it is likely pretty ugly (e.g. get the resource for the current class, look at the schema for the URL, if it's `file::` read the contents of the directory, if it's `jar::` parse the jar file location and programmatically open it, etc.). Is it feasible to pack the images into a zip file (or similar) and include that file instead of the folder containing the images? – James_D Aug 08 '17 at 16:11
  • Well i changed the code according with James_D comment. – Pedro Figueiredo Aug 08 '17 at 21:21
  • i put a list of names related to the image files (png) in a text file called "lista_icones.txt". I can see the images when i run the app from NetBeans, but when i run the JAR file it doesn't show me those images. Maybe is because i'm using FileReader, but what can i use instead of it in the BufferedReader? – Pedro Figueiredo Aug 08 '17 at 21:27
  • I changed the line: BufferedReader br = new BufferedReader(new FileReader("src/imagens/lista_icones.txt")); to: InputStream inputStream = new FileInputStream("src/icones/lista_icones.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); but still doesn't appear the images when i run the JAR in the desktop :( – Pedro Figueiredo Aug 08 '17 at 21:37
  • It seems highly unlikely you would have a folder called `src` available at runtime (especially when running from a jar file), doesn't it? You need to create the stream from a resource to read the file, the same way you did in the original post. – James_D Aug 08 '17 at 22:12
  • Ok i just got it, many thanks for you guys for your help. I leave the final code here, could be of some help for someone else. :) – Pedro Figueiredo Aug 09 '17 at 14:50

0 Answers0