venerdì 8 ottobre 2010

Just a little bit about Files.....

Some time (many times), you need to deal with files.....
One important thing to remembar when you manage files in Java is that the object "File" (java.io.File) is not a fisical file but a reference to a potential not necessary existing file.
When you declare a File object as in :

File fp = new File("nomeFile.txt");

You are not "opening" the file "nomeFile.txt", but preparing yourself to deal with a file (or bettar a file system object) whos name should be that.
Infact the file could still not be there, or it could be something different from a file (a directory? yes! but evene other objects).

Take a look at the following code:


import java.io.*;

public class FileTest {
  public static void main(String args[]) {
    File f = new File(args[0]); 
String msg = "";
    if (f.exists())
       msg = " is found";
    else
       msg = " does not exists";
System.out.println(f + msg);
  }
}

This code explains that when you create a File object you may not yet be handling the file itself, and you can manage the existence of it.
Have fun guys !

Nessun commento:

Posta un commento