14
File Input / Output

File Input / Output

  • Upload
    helena

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

File Input / Output. Files. Persistent storage So your application can ‘remember’ Similar methods to reading / displaying information as before, but a different ‘destination’. Scanner Class. Before, we read data from ‘standard input’ Scanner myScanner = new Scanner (System.in); - PowerPoint PPT Presentation

Citation preview

Page 1: File Input / Output

File Input / Output

Page 2: File Input / Output

FilesPersistent storage

So your application can ‘remember’

Similar methods to reading / displaying information as before, but a different ‘destination’

Page 3: File Input / Output

Scanner ClassBefore, we read data from ‘standard input’

Scanner myScanner = new Scanner (System.in);

System.in will read from the keyboard

We can pass Scanner other Objects to read from other sources…

Page 4: File Input / Output

Scanner ClassNow we use the FileReader class

Don’t forget your importsjava.utiljava.io

Scanner myScanner = new Scanner (new FileReader(“c:\\myfolder\\myFile.txt”);

Page 5: File Input / Output

FileReaderScanner myScanner = new Scanner (new FileReader(“c:\\myfolder\\myFile.txt”);

Some Things to notice:

We create a FileReader Object “on the fly” It doesn’t even have a name

The double slashesSlash is the escape character

PathsRelative vs. AbsoluteDrive letters

Page 6: File Input / Output

FileReaderNow we can use the same scanner

methodsnextInt( )nextDouble( )hasNext( )

Page 7: File Input / Output

ExampleScanner myScanner = new Scanner (new FileReader(“c:\\myFile.txt”));

String firstName, lastName;

double hoursWorked, payRate, wages;

firstName = myScanner.next( );

lastName = myScanner.next( );

hoursWorked = myScanner.nextDouble( );

payRate = myScanner.nextDouble( );

wages = hoursWorked * payRate;

myScanner.close( );

Page 8: File Input / Output

Writing to Files

Lots of different “Writer and Reader” classes.

PrintWriter is a good, high level class for reading general data from a file

Page 9: File Input / Output

Writing to Files

PrintWriter myPW = new PrintWriter(“c:\\myFile.out”);

Can use the familiar output methods on the fileprint( )println( )

myPW.println(“The pay is: $” + pay);This stores the text in the file “myFile.out”

Page 10: File Input / Output

Closing Files

Don’t forget to close your files!

Saves memory and…

“Flushes the buffer”

Page 11: File Input / Output

Files

What happens if something goes wrong?

Trying to read a file that isn’t there…

Trying to write to a location that doesn’t exist…Like the ‘Z:’ drive

Trying to write to a file that is already there…By default, it overwrites the file.This may not be what you want!

Page 12: File Input / Output

Files

If something goes wrong, the statement that causes a problem throws an exception

One of two things must happen in a method that causes an exception

You deal with it

Or… pass the problem up to the calling method

Page 13: File Input / Output

ExceptionsFor now, we will ‘pass the buck’ and let the

system deal with the potential error

public static void main (String [] args) throws FileNotFoundException {

. . .

// no try – catch block in here

}

Page 14: File Input / Output

Files

In the real-world, files are useful, but many real world data tasks are handled via a database

mySQL

Websites use a database to holdProductsCommentsReviews