Question:

Java query - testing file extension when opening a file?

by  |  earlier

0 LIKES UnLike

G'day,

I'm fairly new to Java (being using C++, C# in the past), I am currently working on an assignment that prompts the user for a filename (from the console) which it then sources and reads data from (.log file), whilst (as Im sure all the Java enthusiasts out there would agree) it is simple enough to create this connection however it has been imposed that only log files are allowed to be opened, up until this point I have been using a basic substring method on the String filename provided to identify whether the last 3 characters in the string are "log", however this method seems a tab trivial and I was wondering if there is a more efficent way of performing this action??

Any guidance would be greatly appreciated,

David

 Tags:

   Report

1 ANSWERS


  1. Windows is really the only OS that uses filename extensions in this way. Thus, the platform-independent Java language doesn't have a built-in way to check filename extensions other than simple substring matching.

    That said, you are probably better off performing some sort of check on the contents of the file than the name, or at least in addition to checking the name.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.