


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Duhadway; Class: Computational Science: JAVA/Internet; Subject: Computer Science; University: Utah State University; Term: Spring 2009;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



public boolean createNewFile() throws IOException Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
Returns: true if the named file does not exist and was successfully created; false if the named file already exists
Boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. boolean isFile() To see if the File is associated to a file or not. If false, it is a directory or is not associated with a file. boolean isDirectory() To see if the File is associated to a file or not. If false, it is a file or is not associated with a directory.
File directory = new File("C:/JavaPrograms/Ch12");
String filename[] = directory.list();
for (int i = 0; i < filename.length; i++) { System.out.println(filename[i]); }
String getAbsolutePath() Returns the absolute pathname string of this pathname. String getCanonicalPath() Returns the canonical pathname string of this pathname. Same as getAbsolutePath(), except that it Removes redundant names such as “.” and “..” Resolves symbolic links under Unix Converts driver letters to uppercase on Windows
String getName() Returns the name of the file or directory denoted by this abstract pathname. String getParent() Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. File getParentFile() Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory. String getPath() Converts this abstract pathname into a pathname string.
JFileChooser chooser = new JFileChooser( );
JFileChooser chooser =
new FileChooser("D:/JavaPrograms/Ch12");
String current = System.getProperty(“user.dir”);
JFileChooser chooser = new JFileChooser(current);
It is possible to store objects just as easily as you store primitive data values. We use ObjectOutputStream and ObjectInputStream to save to and load objects from a file.
To save objects from a given class, the class declaration must include the phrase implements Serializable. For example,
class Person implements Serializable {
... }
File outFile = new File("objects.data"); FileOutputStream outFileStream = new FileOutputStream(outFile); ObjectOutputStream outObjectStream= new ObjectOutputStream(outFileStream);
Person person = new Person("Mr. Espresso", 20, 'M'); outObjectStream.writeObject( person );
account1bank1 = new Account= new Bank();();
outObjectStream.writeObject( account1 ); outObjectStream.writeObject( bank1 );
Can save objects from the different classes.
File inFile= new File("objects.data"); FileInputStream inFileStream= new FileInputStream(inFile); ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
Person person = (Person) inObjectStream.readObject( );
Account account = (Account) inObjectStream.readObject( ); Bank bank = (Bank) inObjectStream.readObject( );
Must read in the correct order.
Must type cast to the correct object type.
Instead of processing array elements individually, it is possible to save and load the whole array at once.
Person[] people = new Person[N]; //assume N already has a value //build the people array
... //save the array outObjectStream.writeObject ( people );
//read the array Person[] people = (Person[]) inObjectStream.readObject( );