Results 1 to 3 of 3
  1. #1
    Vorsaykal is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Reading In File Names

    I was wondering if I would be able to read in the name of files and add them to an array. So if I had 5 files in a folder called "File1.m4a", "File2.m4a", "File3.m4a", "File4.m4a", "File5.m4a", the program would set array "fileNames" as fileNames[0] = "File1", fileNames[1] = "File2" etc. (It would be fine if it read them in as "File1.m4a", I could always change them, but if there is a way to skip, then that would be better.)

    Is there an easy way to do this?
    Last edited by Vorsaykal; 11-27-2011 at 02:58 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Reading In File Names

    Is there an easy way to do this?
    I'm not 100% sure of the situation - for instance is the extension you want to remove always ".mp4"? Are there (or might there be) other files in the directory besides those that end in this extension?

    In any case you might want to look at some of the methods in the File class. Notice that there are methods for obtaining the names of files in a given directory, and that, if need be, this can be restricted (filtered) based on the filename.

    As for changing the strings you obtain (removing the extension), you should start with the methods offered by the String class: specifically those methods that allow you to find the index of the last occurrence of the '.' character and to obtain a substring, since these will help with identifying and removing the extension.

    Notice also that there is a string method - endsWith() - that will help you identify those filenames within the directory that actually end with the suffix you are interested in. It may simplify the process of removing the suffix to know that all the strings end the same way.

    -----

    What you are trying to do involves a number of steps. Based on the methods provided by the String and File classes figure out a precise strategy (plan). Then implement it one step at a time, posting if you get stuck. (with your code and the problem that has arisen with it, and also some description of the strategy you are using.)

  3. #3
    Vorsaykal is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Reading In File Names

    The extensions don't really matter, like I said, I can remove them if they come with the name of it. That's not what my issue is.

    I have absolutely no clue how to get the names of a file in a folder. I looked through File (Java Platform SE 6) and found a method called listFiles, which might do what I need, but I really have no clue how to use it.

    If I have a folder at "C:\Users\User\Folder" with 5 files in it, all I really need is any way to get those file names, with or without the extensions into my program. Whether it saves them into an array, each into their own individual string, doesn't matter. I should be able to get what I need done when I have them in my program, I just don't have a clue how to get them there.

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2011, 06:15 AM
  2. Replies: 6
    Last Post: 04-16-2011, 08:06 PM
  3. Replies: 8
    Last Post: 02-20-2009, 12:40 PM
  4. Adding file names(Help!!)
    By Gambit17 in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 07:26 AM
  5. building file and variable names from variables
    By madad2005 in forum New To Java
    Replies: 2
    Last Post: 07-18-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •