java.io
https://docs.oracle.com/javase/tutorial/essential/io/index.html
https://docs.oracle.com/javase/tutorial/essential/io/streams.html
Streams abstract away from the data source / destination.
they can be anything that holds, generates, or consumes data: disk files, another program, a peripheral device, a network socket, or an array.
All classes are built on top of Byte Streams:
InputStream
and
OutputStream
.
Streams müssen immer mit
close();
geschlossen werden wenn sie nicht
autoclosable
sind, sonst treten IOExceptions auf.
Beispiel
Angenommen wir haben ein Dokument, dass aus rohen Daten besteht:
Dokument [rohe Daten] →
FileInput / FileOuput [ungepuffert, rohe Daten] →
Angabe des Kodierungstyps + InputStreamReader / InputStreamWriter [ungepuffert, Zeichen] →
BufferedReader / BufferedWriter [gepuffert, Zeichen]
// [gepuffert] [ungepuffert] [Datei]
BufferedReader reader = new BufferedReader( new FileReader ( new File (path) ) );BufferedReader reader = null;
FileReader fr = null;//check if path leads to file
try {
File file = new File(path);
fr = new FileReader(file);
reader = new BufferedReader(fr);} catch (FileNotFoundException e){
System.err.println(" Error: File not found. " + e.getMessage());
e.printStackTrace();//CelestialBodyCollection will stay unchanged
System.exit(0);} catch (IOException e) {
System.err.println(" Error: Invalid day argument " + e.getMessage());
e.printStackTrace();//CelestialBodyCollection will stay unchanged
System.exit(0);
}System.out.println("> File opening successful");