OO Programming with Java


File (and directories)

All the variables live in memory and disappear when computer in turn off.
To make them persistent, they have to be saved into file (and conversely read from file).
In Java, all the elements from a files' structure are represented by an instance of File as illustrated below:

try {
  File file = new File("/home/thiry/eclipse-workspace/Course/catalog.csv");
  if (! file.exists())
    file.createNewFile();
  File parent = new File(file.getParent());
  String[] files = parent.list();
  File file0;
  for(String filename : files) {
    file0 = new File(filename);
    if(! file0.isDirectory()) System.out.println(file0);
  }
} catch (Exception e) {
  e.printStackTrace();
}

Exercise:


6 - 12