File Input and Output - Lecture Slides | CS 3410, Study notes of Computer Science

Material Type: Notes; Professor: Duhadway; Class: Computational Science: JAVA/Internet; Subject: Computer Science; University: Utah State University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/31/2009

koofers-user-tfr-1
koofers-user-tfr-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2/19/2009
1
Chapter 12 - continued
File Input and Output
The createNewFile Method
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
Using createNewFile
File outStream = new File("data.out");
try{
outStream.createNewFile();
}
catch(IOException e){
System.out.println(“Error opening file");
}
Some File Methods
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 f ile 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 f ile or not. If false, it is a file
or is not associated with a directory.
Note that you can create a File object that does not
yet exist. Both isDirectory() and isFile() will return
false
Listing Files in a Directory
List the name of all files in the directory
C:\JavaProjects\Ch12
File directory = new File("C:/JavaPrograms/Ch12");
String filename[] = directory.list();
for (int i = 0; i < filename.length; i++) {
System.out.println(filename[i]);
}
Getting The Absolute
Pathname
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
pf3
pf4

Partial preview of the text

Download File Input and Output - Lecture Slides | CS 3410 and more Study notes Computer Science in PDF only on Docsity!

Chapter 12 - continued

File Input and Output

The createNewFile Method

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

Using createNewFile

File outStream = new File("data.out");

try{

outStream.createNewFile();

catch(IOException e){

System.out.println(“Error opening file");

Some File Methods

 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.

 Note that you can create a File object that does not

yet exist. Both isDirectory() and isFile() will return

false

Listing Files in a Directory

  • List the name of all files in the directory C:\JavaProjects\Ch

File directory = new File("C:/JavaPrograms/Ch12");

String filename[] = directory.list();

for (int i = 0; i < filename.length; i++) { System.out.println(filename[i]); }

Getting The Absolute

Pathname

 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

Getting Pathname

Components

 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.

Read / Write Access

 boolean canRead()

Tests whether the application can read

the file denoted by this abstract pathname.

 boolean canWrite()

Tests whether the application can

modify the file denoted by this abstract

pathname.

The JFileChooser Class

javax.swing.JFileChooser

 Can produce a pop-up window that displays the

computers directory structure

 The user can move through this directory and select

a file from it

 This file can then be associated with a File object in

the application

Creating a JFileChooser Object

 A javax.swing.JFileChooser object allows the

user to select a file.

JFileChooser chooser = new JFileChooser( );

 To start the listing from a specific directory:

JFileChooser chooser =

new FileChooser("D:/JavaPrograms/Ch12");

Creating a JFileChooser Object

  • To start the listing from the current directory you can get a system property that returns a string that is the path to the current directory:

String current = System.getProperty(“user.dir”);

  • Then we can use that String as the parameter when we create a JFileChooser:

JFileChooser chooser = new JFileChooser(current);

  • This will set the beginning path of the JFileChooser to the current directory.

Object File I/O

 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 {

... }

Saving Objects

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.

Reading Objects

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.

Saving and Loading Arrays

 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( );