java.io - streams and files
- 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
- 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
- 27. marcello.thiry@gmail.com
String fileName = "c:/temp/file.exe";
int byteValue;
try {
InputStream in = new FileInputStream(fileName);
while ((byteValue = in.read()) != -1) {
System.out.format("[%2X]n", byteValue);
}
in.close();
}
catch (IOException ex) {...}
- 28. marcello.thiry@gmail.com
String fileName = "c:/temp/file.exe";
int byteValue;
try {
InputStream in = new FileInputStream(fileName);
while ((byteValue = in.read()) != -1) {
System.out.format("[%2X]n", byteValue);
}
in.close();
}
catch (IOException ex) {...}
- 30. marcello.thiry@gmail.com
Try-with-resources
String fileName = "c:/temp/file.exe";
byte[] bytes = new byte[500];
int read;
try {
try (InputStream in = new FileInputStream(fileName)) {
while ((read = in.read(bytes)) != -1) {
System.out.format("%d bytes read:n", read);
for (int i = 0; i < read; i++) {
System.out.format("[%2X]", bytes[i]);
}
System.out.println();
}
}
} catch (IOException ex) {...}
- 31. marcello.thiry@gmail.com
Try-with-resources
String fileName = "c:/temp/file.exe";
byte[] bytes = new byte[500];
int read;
try {
try (InputStream in = new FileInputStream(fileName)) {
while ((read = in.read(bytes)) != -1) {
System.out.format("%d bytes read:n", read);
for (int i = 0; i < read; i++) {
System.out.format("[%2X]", bytes[i]);
}
System.out.println();
}
}
} catch (IOException ex) {...}
- 35. marcello.thiry@gmail.com
String fileName = "d:/downloads/mynewfile.txt";
try {
try (OutputStream out = new FileOutputStream(fileName)) {
byte[] bytes = new byte[]{'T', 'E', 'S', 'T', 32, 0x41};
out.write(bytes);
}
} catch (IOException e) {...}
- 40. marcello.thiry@gmail.com
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.dat"))) {
out.writeObject(new User("a", "a"));
out.writeObject(new User("b", "b"));
out.writeObject(new User("c", "c"));
out.flush();
} catch (IOException e) {...}
User u;
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user.dat"))) {
for (int i = 0; i < 3; i++) {
u = (User) in.readObject();
System.out.println(u.getLogin() + ", " + u.getPassword());
}
} catch (IOException | ClassNotFoundException e) {...}
- 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
- 49. marcello.thiry@gmail.com
String fileName = "temp.txt";
String line;
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
- 50. marcello.thiry@gmail.com
String fileName = "temp.txt";
String line;
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
- 51. marcello.thiry@gmail.com
String fileName = "temp.txt";
String line;
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
- 52. marcello.thiry@gmail.com
String fileName = "temp.txt";
String line;
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
- 53. marcello.thiry@gmail.com
String fileName = “temp.txt";
String line;
try {
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
} catch (FileNotFoundException ex) {...
} catch (IOException ex) {...
}
- 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
- 58. marcello.thiry@gmail.com
try {
FileInputStream in = new FileInputStream("c:/temp/fileUTF16.txt");
InputStreamReader inReader = new InputStreamReader(in, "UTF-16");
try (BufferedReader buffReader = new BufferedReader(inReader)) {
int character;
while ((character = buffReader.read()) != -1) {
System.out.print((char) character);
}
}
} catch (IOException e) {...}
- 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
- 62. marcello.thiry@gmail.com
String fileName = “temp.txt";
FileWriter fileWriter = new FileWriter(fileName);
FileWriter fileWriter = new FileWriter(fileName, false);
FileWriter fileWriter = new FileWriter(fileName, true);
- 63. marcello.thiry@gmail.com
String fileName = "c:/temp.txt";
try {
try (FileWriter fileWriter = new FileWriter(fileName)) {
fileWriter.write("My first line");
fileWriter.write("rn"); // new line - windows
fileWriter.write("My second line");
}
} catch (IOException e) {...}
- 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) {}
- 70. marcello.thiry@gmail.com
String fileName = "c:/temp/MyNewFile.txt";
try {
FileOutputStream out = new FileOutputStream(fileName);
OutputStreamWriter outWriter = new OutputStreamWriter(out, "UTF-16");
try (BufferedWriter buffWriter = new BufferedWriter(outWriter)) {
buffWriter.write("UNICODE text");
buffWriter.newLine();
buffWriter.write("Some more...");
}
} catch (IOException e) {...}