Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting a filename

 
Pisces Iscariot
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to make a database of an mp3 collection where all the mp3's are actually .txt files with nothing in them. I have to put them in some kind of collection which shouldnt be a problem for me. The problem is that I don't know how to get the file and folder names themselves in order to add them to the database. So folder names would be like "Rolling Stones - 40 Licks" with file names within being "01 - Same Old Crap.txt" I have no idea where to start on this, any help is appreciated, thanks!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the java.io.File class. It has methods to let you list all files and directories within a directory and methods to determine whether it is a file or directory.
 
Kia Phia Ben
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think
File file = new File(name);
name is file name or folder.

and detect file music follow by
if(file.isFile()){

if( file.getName().indexOf(".mp3") > 0);
System.out.print("this is a file mp3");
}
 
Sam Codean
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct but a small addition if you do not mind

=====================
I think
File file = new File(name);
name is file name or folder.

and detect file music follow by
if(file.isFile()){

if( file.getName().indexOf(".mp3") > 0);
System.out.print("this is a file mp3");
}

======================================
File file = new File(name);

if(file.isFile()){
String fileName = file.getName();

if( (fileName.lastIndexOf(".mp3")+1) ==
(fileName.length()-".mp3".length())
)
System.out.print("this is a file mp3");
}


Even better is to place the extension in a Constant and use that.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, I would suggest that last part could be

or possibly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic