Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Generic file name validation code?

Thread view: 
harry - 02 Sep 2007 12:19 GMT
I need to validate a file name but it has to work on Windows & Unix!

Does anybody know of java code that does this?

thanks in advance

harry
Andrew Thompson - 02 Sep 2007 13:49 GMT
>I need to validate a file name but it has to work on Windows & Unix!
>
>Does anybody know of java code that does this?

I would rely on the Java plug-in itself.

<sscce>
import java.io.File;

public class FileNameTest {

 public static void checkFileName(String name) {
   try {
     File file = new File(name);
     file.createNewFile();
     System.out.println( "File established: " + name );
   } catch (Exception e) {
     e.printStackTrace();
   }
 }

 public static void main(String[] args) {
   String name;

   name = "$haha";
   checkFileName(name);

   name = "?$haha";
   checkFileName(name);

   name = "/haha";
   checkFileName(name);
 }
}
</sscce>

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Robert Larsen - 03 Sep 2007 07:14 GMT
> I need to validate a file name but it has to work on Windows & Unix!
>
[quoted text clipped - 3 lines]
>
> harry

This sounds easy to write using the java.util.regex package.
How should a file name look like ?
Mark Rafn - 03 Sep 2007 19:32 GMT
>I need to validate a file name but it has to work on Windows & Unix!

What's "it" in that sentence?  The code or the filename?  It's very easy to
make the code work in multiple OSs, of course.  It's very hard to make a
perfect validator to ensure that a filename would be valid in, say, the
Japanese release of Windows 95 when running under Solaris with an 8859-1
default encoding.

>Does anybody know of java code that does this?

java.io.File forces some amount of well-formedness, and getCanonicalFile()
will normalize it using OS-specific code.  If you try to actually create or
read the file, you'll be certain that the path is valid.

You won't know if it would be valid on another OS, but you really can't
know that anyway - different paths, drive letter conventions, permissions,
conflicting files, etc, even on the same OS on another machine will break.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.