Results 1 to 3 of 3
Thread: Reading In File Names
- 11-27-2011, 02:55 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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.
- 11-27-2011, 03:13 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Reading In File Names
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?Is there an easy way to do this?
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.)
- 11-27-2011, 03:30 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
-
Return search file contents instead of the file names
By rnamboodiri in forum LuceneReplies: 2Last Post: 12-05-2011, 06:15 AM -
how to get file names from a directory or package in eclipse ?
By pooja123 in forum New To JavaReplies: 6Last Post: 04-16-2011, 08:06 PM -
comparing strings in notebook to names of a file
By debz in forum New To JavaReplies: 8Last Post: 02-20-2009, 12:40 PM -
Adding file names(Help!!)
By Gambit17 in forum New To JavaReplies: 4Last Post: 11-09-2007, 07:26 AM -
building file and variable names from variables
By madad2005 in forum New To JavaReplies: 2Last Post: 07-18-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks