Credits

Thursday, January 13, 2011

Java's File Class

Like any computing, inputs and outputs of data and information come in volume and value and with these, they often are stored in files for future use and reference.

In Java, File  and RandomAccessFile classes are used to display and store user inputs and outputs. File class however is solely used for displaying file information (e.g size, location, date).

To use the File class, we must invoked java.io package. Find below a program I found in creating an object of File class.

[+/-] show/hide


import java.io.*;
public class FileCreatingSample {

private static void doCreate() {

// Create a File object
File file = new File("\\Documents and Settings\\Rose\\My Documents\\NetBeansProjects\\SampleExercises\\src\\myuyabs.txt");

boolean success = false;

try {
// Create file on disk (if it doesn't exist)
success = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

if (success) {
System.out.println("File did not exist and was created.\n");
} else {
System.out.println("File already exists.\n");
}


}


/**
* Sole entry point to the class and application.
* @param args Array of String arguments.
*/
public static void main(String[] args) {
doCreate();
}

}

AddThis