Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

identify the file extension using java

Tags:

java

i have different format files in DB. i want to copy to my local machine.

how can i identify the file format (doc, xls, etc...)

Regards, krishna

Thanks, for providing suggestions... based on your suggestions i had written the code & i am completed...

please look into my blog.. i posted the code over here...

http://muralie39.wordpress.com/java-program-to-copy-files-from-oracle-to-localhost/

Thank you guys..

Thanks, krishna

like image 609
krishna Avatar asked Dec 28 '22 04:12

krishna


1 Answers

If your files are named according to convention, you can just parse the filename:

String filename = "yourFileName";
int dotPosition = filename.lastIndexOf(".");
String extension = "";
if (dotPosition != -1) {
    extension = filename.substring(dotPosition);
}
System.out.println("The file is of type: " + extension);

That's the simplest approach, assuming your files are named using some kind of standard naming convention. The extensions could be proprietary to your system, even, as long as they follow a convention this will work.

If you need to actually scan the file to get the format information, you will need to do some more investigation into document formats.

like image 123
karlgrz Avatar answered Jan 10 '23 08:01

karlgrz