Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:10:29+00:00 2026-05-31T01:10:29+00:00

I am currently working on a project for school, it is Java based and

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:10:30+00:00Added an answer on May 31, 2026 at 1:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a school project in Eclipse (We have just started using
I'm currently working on a project using the current if statement and i'm sure
I am currently working Java project with use of apache poi. Now in my
I am currently working on a project for school that is a java memo
I am currently working on a project developed using Zend Framework, based on the
I am currently working on a school project where the assignment, among other things,
I am currently working on a project in java. I need to create a
I'm currently working on some school project; we are developing a simple RPG, but
I am currently working on a project that hosts WinForms designer. I am using
I am currently working on a project for school which will be a webapp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.