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;
}
In Java, the
Fileclass does not necessary represent an “existing” file on the file system. For example:Also, the class resolves the file from the current working directory. You may get this directory with
In your case, if you can, I would suggest getting a
File[]array with :which will return an array of
Fileobjects 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 :Also, with your list of files (
String[]), you can create an icon using :or with your list of files (
File[]), it is cleaner :Good luck.