A Funny Java Flavoured Look at the World

Wednesday, June 21, 2006

Using the File class

I was doing some work on checking a directory for a file and then processing the file. This meant I had to use the Java's File class. It's a bit wired to start with having an object for a File especially in Java because File can be either a directory or file. I'm not sure whether having the File class be a directory or file is a good idea because it often means you have to check if a String directory path has created a File which is a directory or a File which is a file.

The File class is very easy to use and does have helpful isFile and isDirectory methods but it seems like you have to some extra checks. The advantage of course is that you can pass a directory and file about under the same Class. Thinking about it as I'm writing it probably isn't that much different from having a Directory and File class which extended an Abstract File class, you would still have to do some checking somewhere.

Don't get me wrong I'm not really moaning about it, I found the File class really good to use and it has some useful methods.

I found a really good page on the File class with useful snippets of code and a brief description of the methods you are likely to use.

http://mindprod.com/jgloss/file.html

I decided to make a static helper file with some methods which combined some of the File objects functions together, like making a directory and then a file and making a File instance from a String of directory and filename, checking it was exists, it was a file (not directory) etc. I'm not really sure about making static helper files but in this case I didn't want an object with an instance, just methods. Also the methods didn't hold any variables just took in a variable and returned a different one.

There are a few gotcha's with the file class. Here are the one's I can think of on top of my head

File can be a directory or a file.

If you pass in Null I think the file exists will return true.

You cannot delete a folder if there is a file(s) inside of it.

When you create a file with a new directory it doesn't create the directory, you have to use the mkdir() or createNewFile() methods

The deleteOnExit function only deletes when you code finishes, so I don't think you can use it between methods (e.g. in a junit test). I'm not totally sure when it deletes the file but I found that between my tests in a junit class the directory and file wasn't deleted.

One other bit of functionality that I like with the File class is each file has a deleteOnExit() function. This is good for writing unit tests or so I thought. I found that the folder or file is only deleted when the code exits. So this meant that I had to write a function which checked for and deleted the file and then folder.

Another interesting function the File has is that it can create unique temporary files and it gives each a file a random number on the front of it.

I quite enjoyed my time with the File class unlike some of the other classes in the Java framework this is very straight forward

3 Comments:

  • The only problem I have with the File class is the rename method. If you use it, then you can drop your object, because it can not reach the file anymore.

    By Anonymous Anonymous, at Wed Jun 21, 10:40:00 am 2006  

  • I totally agree with you...

    By Blogger CARFIELD, at Thu Jun 22, 11:45:00 am 2006  

  • I've had problems before when i wanted to read a file from a jar. The only way to do this is to get an inputstream form the class.getResource(String s), as the file class doesn't know about zip /jar. But my library required a file, since it had to read twice (the stream gets closed the first time due to a charset exception). This is avoidable with a temp file, or throwing the exception up and handling it there, but it is ugly.

    By Anonymous Anonymous, at Mon Jun 26, 04:07:00 pm 2006  

Post a Comment

<< Home