Beruflich Dokumente
Kultur Dokumente
Files and
Streams
Reading Characters
Reading Characters
When we say we want to read characters, it
means we never want to move things like
images.
Each of the bubbles in the list below
represents a Java class that is designed to
read a certain type of character. Each of
these is designed for a particular case.
Writing Characters
Writing Characters
When we are writing characters, the same
idea applies.
Each of the bubbles in the list below
represents a Java class that is designed to
write a certain type of character. Each of
these is designed for a particular case.
Reading Bytes
Reading Bytes
Below is the list of classes you use when
you want to read at a finer grain than just
characters. That would be bytes.
InputStream is an abstract
Writing Bytes
Writing Bytes
Below is the list of classes you use when
you want to write bytes.
OutputStream is an abstract
General Approach
General Approach
Inevitably, when you sit down to read from
a file, you have to sort through the choices on
those lists.
The best approach is to pick one from
either listcharacter and byteand learn to
use it.
Reading Characters
from a File
Reading Bytes
from a File
Alternatives for
Efficiency
Now, we have
added a
BufferedReader,
which allows us
to read a line at
a time.
The
BufferedWriter
also allows us
to write an
entire String
Here, we see
that an entry
of spaces is
the sentinel
value.
}
System.out.println( "stuffRead=" + stuffRead.toString() );
}
catch( IOException io )
{
System.out.println( "ConsoleReader Constructor threw an IOException, io=" + io );
}
File Serialization
throws
Shortly,
File Serialization
Finally, lets take a look at the file that was
written by the last screen:
File Serialization
The process to read from an existing
Serialized file is very similar.
File myFile = new File( C:/myFile.ser );
FileInputStream in = new FileInputStream( myFile);
ObjectInputStream s = new ObjectInputStream(in);
String myString = (String)s.readObject();
Reading
Assignments
-Making Directories
- Copying, Moving Files
-Listing Directories