Object-oriented programming Undergraduate Course Presentations
java.io streams and files in Java
University of Vale do Itajaí
Univali
Incremental Tecnologia
English version
4. marcello.thiry@gmail.com
if you do not know much about
charsets, code pages, encoding,
ASCII, UNICODE, etc.
Before we start…
Take a look in this article
http://www.joelonsoftware.com/articles/Unicode.html
The Absolute Minimum Every Software Developer Absolutely,
Positively Must Know About Unicode and Character Sets (No Excuses!)
by Joel Spolsky
It’s a bit old, but a good start
5. marcello.thiry@gmail.com
Set of characters you can use
Charset Repertoire
ISO-8859-1 - Western Alphabet ISO-8859-5 - Cyrillic Alphabet
JIS X 0208 - Japanese Alphabet ISO-8859-7 - Greek Alphabet
14. marcello.thiry@gmail.com
Region of a physical memory
storage used to temporarily store
data while it is being moved from
one place to another
Data Buffer
15. marcello.thiry@gmail.com
Conversion of an object to a series
of bytes, which lets a program
write whole objects out to streams
and read them back again
Object Serialization
16. Set of routines, protocols, and
tools to access a software
component/module without the need
to know details of its
implementation
Application Programming*
Interface API
*Also used: program
22. marcello.thiry@gmail.com
read() reads a single byte
read(byte[] b) reads b.length bytes into an array
read(byte[] b, int off, int len) reads len bytes into an array,
starting from the position off
skip(long n) skips discards n bytes
close() closes the stream
https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html
23. marcello.thiry@gmail.com
mark(int readlimit) marks the current position in
this input stream
reset() repositions this stream to the position at
the time the mark method was last called
https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html
And, if markSupported()...
24. marcello.thiry@gmail.com
write(int b) writes a single byte
write(byte[] b) writes b.length bytes from the array
write(byte[] b, int off, int len) writes len bytes from the array
starting at offset off
flush() forces any buffered output bytes to be written
out
45. marcello.thiry@gmail.com
class Reader
Abstract class for reading
character streams
read(): reads a single character
read(char[]): reads characters into an array
skip(long): skips N characters
close(): closes the stream
https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html
54. marcello.thiry@gmail.com
But what about the class
InputStreamReader?
https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html
Reads bytes and decodes them into
characters using a specified charset
Bridge from byte streams to
character streams
60. marcello.thiry@gmail.com
class Writer
Abstract class for writing to
character streams
write(int): writes a single character
write(char[]): writes an array of characters
write(String): writes a string
close(): closes the stream
https://docs.oracle.com/javase/8/docs/api/java/io/Writer.html
65. marcello.thiry@gmail.com
String fileName = "c:/temp/MyFile.txt";
try {
FileWriter writer = new FileWriter(fileName, true);
try (BufferedWriter buffWriter = new BufferedWriter(writer)) {
buffWriter.write("My first line");
buffWriter.newLine();
buffWriter.write("My second line!");
}
} catch (IOException e) {...}
66. marcello.thiry@gmail.com
But what about the class
OutputStreamWriter?
https://docs.oracle.com/javase/8/docs/api/java/io/OutputStreamWriter.html
Characters written to it are encoded
into bytes using a specified charset
bridge from character to byte
streams
68. marcello.thiry@gmail.com
try {
OutputStreamWriter outWriter = new OutputStreamWriter(System.out);
try (BufferedWriter buffWriter = new BufferedWriter(outWriter)) {
buffWriter.write("Printing a line on the console");
buffWriter.newLine();
buffWriter.write("Printing a second line...rn");
}
} catch (IOException e) {}