1

I am currently working on a project for school, it is Java based and I am using Eclipse on Linux Mint to write it. The assignment says use the statement String[] filenames = new java.io.File("icons).list(); to create an array of file names.

The problem is I am not sure what to do with this, I have spent the past few hours searching the Internet and my textbook, but to no avail. Does it need to be a separate method?

Below is my guess for the needed code in the model (the project is to make a matching game, with a GUI) the names will have to be converted later on into actual icons, but I am pretty sure I have that part figured out, I just can't seem to get the darn files into the array!!

Thanks in advance,

public String[] list() {
    String[] fileNames = new java.io.File("icons").list();
    return fileNames;
}
3
  • 1
    try putting the path to the directory inside the quotes where icons is. Commented Mar 5, 2012 at 0:15
  • 1
    I'm not sure what your question is. The code you posted returns the names of all files in the "icons" directory as an array. Is the question, "How do I read the contents of each individual file?" Commented Mar 5, 2012 at 0:16
  • A little more detail then, right now I have 2 classes, the model, and the actual UI. My ultimate goal is to have the icons in the folder put into an array of file names (located in the model), and then assigned to an array of JButtons(located in the GUI) ( I have to make one of those tile memory match games) Basically I need the model part to read the folder "icons", put it in an array, then put it into a 2D array representing the button grid, and finally assign the files to JButtons. Commented Mar 5, 2012 at 0:22

3 Answers 3

1

In Java, the File class does not necessary represent an "existing" file on the file system. For example:

File f = new File("some_unknown_unexisting_file.bob");
System.out.println(f.exists());  // most likely will print 'false'

Also, the class resolves the file from the current working directory. You may get this directory with

System.out.println(new File(".").getAbsolutePath());

In your case, if you can, I would suggest getting a File[] array with :

File[] files = new File("icons").listFiles(new FileFilter() {
   @Override
   public boolean accept(File f) {
      return !f.isDirectory() && f.canRead();
   }
});
for (File f : files) {
   System.out.println(f.getAbsolutePath());
}

which will return an array of File objects which are not folders and that you can open for reading (note that this is not always true, but is just fine in your case).

But if you have to use list(), then this is equivalent :

File parent = new File("icons");
String[] fileStr = parent.list(new FilenameFilter() {
   @Override
   public boolean accept(File dir, String name) {
      File f = new File(dir, name);
      return !f.isDirectory() && f.canRead();
   }
});
for (String f : fileStr) {
   System.out.println(new File(parent, f).getAbsolutePath());
}

Also, with your list of files (String[]), you can create an icon using :

String filename = fileStr[i];  // some file name within the array
ImageIcon icon = new ImageIcon("icons" + File.separator + filename); 

or with your list of files (File[]), it is cleaner :

File file = files[i];   // some file within the File[] array
ImageIcon icon = new ImageIcon(file.getAbsolutePath());

Good luck.

Sign up to request clarification or add additional context in comments.

3 Comments

This definitely helps with my understanding of what a file is, basically it's a reference to a String containing the path? Then I assume to actually get the icon on a button, I just have to make a new ImageIcon. However, I'm still a little unclear on how to actually read the file names, the getAbsolutePath gave me the directory "/home/daniel/workspace/Assignment3/" but I'm running this on Linux, and the TA will be using a windows running JCreator most likely.
+1 for FilenameFilter, also see FileFilter.
If you're using Eclipse, then the default path is the eclipse's project path, so regardless to the OS used, it shouldn't pose any problem as this is a default platform independent behavior. A File object is not only a reference to a path name, but like an utility to get information about the path. Creating a new File does not create or open anything, but gives you a set of "operations" that you can perform using the given path name. JButton has a constructor that can take an Icon so, yes, you create a new ImageIcon and feed it to the JButton constructor.
0

The code you wrote looks okay. Are you sure the folder "icons" exists where Java is looking?

Try this:

File f = new File("icons");
System.out.println("Does icons exist?" + f.exists());
System.out.println("Is it a dir?" + f.isDirectory());
System.out.println("How many files does it contain?" + f.list().length);

Good luck!

Comments

0

I've had the same problem. When I tried moving the icons folder into the folder just before the src folder, it seems to work. Not sure what I will do when I submit the assignment, as for it to work in JCreator, I believe it has to be with the .java files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.