Download Files and Streams Part1-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!
Chapter 16 – Files and
Streams
Goals
To be able to read and write text files
To become familiar with the concepts of text
and binary formats
To learn about encryption
To understand when to use sequential and
random file access
To be able to read and write objects using
serialization
MEM
CPU
HDD
keyboard
monitor terminal console
standard input stream
standard output stream file input stream LOAD READ file output stream SAVE WRITE
Streams files
What does information
travel across?
docsity.com
Reading and Writing Text Files
Text files – files containing simple text
Created with editors such as notepad, html, etc.
Simplest way to learn it so extend our use of
Scanner
Associate with files instead of System.in
All input classes, except Scanner, are in java.io
import java.io.;*
Numerical Input
2 ways (we’ve learned one, seen the other)
Use int as example, similar for double
First way:
Use nextInt()
int number = scanner.nextInt();
Second way:
Use nextLine(), Integer.parseInt()
String input = scanner.nextLine(); int number = Integer.parseInt(input);
Numerical Input
Exceptions
nextInt() throws InputMismatchException
parseInt() throws NumberFormatException
Optimal use
nextInt() when there is multiple information on
one line
nextLine() + parseInt() when one number
per line
Reading Files
To read from a disk file, construct a FileReader
Then, use the FileReader to construct a Scanner
object
FileReader rdr = newFileReader("input.txt");
Scanner fin = new Scanner(rdr);
Reading Files
You can use File instead of FileReader
Has an exists() method we can call to avoid
FileNotFoundException
File file = new File ("input.txt");
Scanner fin;
if(file.exists()){
fin = new Scanner(file);
} else {
//ask for another file
}
File Class
java.io.File
associated with an actual file on hard drive used to check file's status
Constructors
File() File(, )
Methods
exists() canRead() , canWrite() isFile() , isDirectory()
File Class
java.io.FileReader
Associated with File object
Translates data bytes from File object into a
stream of characters (much like InputStream vs.
InputStreamReader)
Constructors
FileReader( );
Methods
read(), readLine()
close() docsity.com
Writing to a File
The out field of the System class is a PrintWriter
object associated with the console
We will associate our PrintWriter with a file now
PrintWriter fout = new PrintWriter("output.txt"); fout.println(29.95); fout.println(new Rectangle(5, 10, 15, 25));
fout.println("Hello, World!");
This will print the exact same information as with
System.out (except to a file “output.txt”)!
Closing a File
Only main difference is that we have to close
the file stream when we are done writing
If we do not, not all output will written
At the end of output, call close()
fout.close();
File Locations
When determining a file name, the default is to
place in the same directory as your .class files
If we want to define other place, use an absolute
path (e.g. c:\My Documents)
in = new FileReader(“c:\homework\input.dat”);
Why \?
Sample Program
Two things to notice:
Have to import from java.io
I/O requires us to catch checked exceptions
java.io.IOException