String, StringBuffer
Math
Datenströme
System
Objekt-Ströme
Zeichenketten mit der Klasse String
.
Diese Objekte sind 'immutable', d.h. sie werden nie modifiziert, sondern immer neu erzeugt.
public final class String extends Object implements Serializable, Comparable String() String(String) String(StringBuffer) String(char[]) char charAt(int) int compareTo(String) // returns -1, 0, +1 String concat(String) boolean endsWith(String) boolean equals(Object) boolean equalsIgnoreCase(String) void getChars(int srcb, int srce, char[] dst, int dstb) int indexOf(String) int length() String replace(char alt, char neu) boolean startsWith(String) String substring(int von) String substring(int von, int bis) char[] toCharArray() String toLowerCase( . ) String toUpperCase( . ) String trim() String valueOf( . )
String UML Diagramm
Zeichenketten mit der Klasse StringBuffer
.
Diese Objekte sind veränderbar.
public final class StringBuffer extends Object implements Serializable StringBuffer() StringBuffer(String) StringBuffer(int) char charAt(int) void getChars(int srcb, int srce, char[] dst, int dstb) int capacity() int length() void setCharAt(int, char) void setLength(int) StringBuffer append( . ) // returns this StringBuffer insert(int, . ) // returns this StringBuffer delete(int, int) // returns this StringBuffer deleteCharAt(int) // returns this StringBuffer reverse() // returns this String substring(int von) String substring(int von, int bis)
StringBuffer UML Diagramm
Die Klasse Math
stellt eine
Reihe von nützlichen Konstanten und Methoden zur Verfügung.
public final class Math extends Object double E double Pi t abs( t ) t = int, long, float, double t max( t, t ) t min( t, t ) acos, asin, atan, atan2, cos, exp, log, sin, sqrt, tan ceil, floor, double pow(double, double) double random() int round(float) long round(double)
Hüllklassen:
Byte
,
Short
,
Integer
und
Long
, so wie
Float
und
Double
.
Super-Klasse:
Number
Langzahl Arithmetik in java.math
:
BigInteger
und
BigDecimal
.
Sämtlicher expliziter Datenaustausch in und aus der JVM (Java Virtual Machine) ist in sogenannte Datenströme organisiert:
Character Ströme
Byte Ströme
Object Ströme
Character Ströme
Byte Ströme
Typische verfügbare Methoden:
read()
(lesen vom Datenstrom),
write()
(schreiben in den Datenstrom),
close()
(Datenstrom schliessen)
import java.io.PrintWriter; import java.io.OutputStreamWriter; public class Screen extends PrintWriter { public Screen() { super(new OutputStreamWriter(System.out),true); } }
import java.io.BufferedReader; import java.io.InputStreamReader; public class KeyBoard extends BufferedReader { public KeyBoard() { super(new InputStreamReader(System.in)); } }
import java.io.IOException; public class Terminal { public static void main(String[] args) { KeyBoard kb = new KeyBoard(); Screen sc = new Screen(); String ea = ""; try { sc.println( "Beenden mit 'ende'"); do { sc.print( "Bitte um Eingabe: "); sc.flush(); ea = kb.readLine(); sc.println("Eingabe ist "+ea); if ( ea == null ) break; } while ( !ea.equals("ende") ); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; public class DateiEin extends BufferedReader { public DateiEin(String name) throws FileNotFoundException { super(new FileReader(name)); } }
import java.io.PrintWriter; import java.io.FileWriter; import java.io.IOException; public class DateiAus extends PrintWriter { public DateiAus(String name) throws IOException { super(new FileWriter(name)); } }
import java.io.IOException; import java.io.FileNotFoundException; public class Dateien { public static void main(String[] args) { Screen sc = new Screen(); if ( args.length < 1 ) { sc.println("Usage: Dateien <eingabe datei> [<ausgabe datei>]"); return; } DateiEin rein; DateiAus raus = null; try { rein = new DateiEin(args[0]); } catch (FileNotFoundException e) { sc.println("Die Datei " + args[0] + " existiert nicht."); return; } if ( args.length >= 2 ) { try { raus = new DateiAus(args[1]); } catch (IOException e) { sc.println("Die Datei " + args[1] + " ist nicht beschreibbar."); } } String ea = ""; int ein = 0; int aus = 0; try { do { ea = rein.readLine(); if ( ea == null ) break; ein++; if ( raus == null ) { sc.println(ea); } else { raus.println(ea); } aus++; } while ( !ea.equals("ende") ); } catch (IOException e) { e.printStackTrace(); } finally { if ( raus != null ) raus.flush(); } try { rein.close(); if ( raus != null ) raus.close(); } catch (IOException e) { e.printStackTrace(); } sc.println(""+ein+" Zeilen von " + args[0] + " gelesen"); if ( args.length >= 2 ) sc.println(""+aus+" Zeilen auf " + args[1] + " geschrieben"); } }
Benutzung:
java Dateien DateiA [DateiB]
import java.io.PrintWriter; import java.io.FileWriter; import java.io.IOException; public class DateiDran extends PrintWriter { public DateiDran(String name) throws IOException { super(new FileWriter(name, true)); } public DateiDran(String name, boolean append) throws IOException { super(new FileWriter(name, append)); } }
import java.io.IOException; import java.io.FileNotFoundException; public class Anhang { public static void main(String[] args) { Screen sc = new Screen(); if ( args.length < 2 ) { sc.println("Usage: Anhang <eingabe datei> <ausgabe datei>"); return; } DateiEin rein; DateiDran dran; try { rein = new DateiEin(args[0]); } catch (FileNotFoundException e) { sc.println("Die Datei " + args[0] + " existiert nicht."); return; } try { dran = new DateiDran(args[1]); } catch (IOException e) { sc.println("Die Datei " + args[1] + " ist nicht beschreibbar."); return; } String ea = ""; int ein = 0; int aus = 0; try { do { ea = rein.readLine(); // sc.println("Eingabe: "+ea); if ( ea == null ) break; ein++; dran.println(ea); aus++; } while ( !ea.equals("ende") ); } catch (IOException e) { e.printStackTrace(); } finally { dran.flush(); } try { rein.close(); dran.close(); } catch (IOException e) { e.printStackTrace(); } sc.println(""+ein+" Zeilen von " + args[0] + " gelesen"); sc.println(""+aus+" Zeilen auf " + args[1] + " geschrieben"); } }
Benutzung:
java Anhang DateiA DateiB
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.net.URL; public class URLEin extends BufferedReader { public URLEin(String url) throws IOException { super(new InputStreamReader( (new URL(url)).openStream() ) ); } }
import java.io.IOException; public class URLs { public static void main(String[] args) { Screen sc = new Screen(); if ( args.length < 1 ) { sc.println("Usage: URLs <eingabe url> [<ausgabe datei>]"); return; } URLEin rein; DateiAus raus = null; try { rein = new URLEin(args[0]); } catch (IOException e) { sc.println("Von URL " + args[0] + " kann nicht gelesen werden."); return; } if ( args.length >= 2 ) { try { raus = new DateiAus(args[1]); } catch (IOException e) { sc.println("Die Datei " + args[1] + " ist nicht beschreibbar."); return; } } String ea = ""; int ein = 0; int aus = 0; try { do { ea = rein.readLine(); if ( ea == null ) break; ein++; if ( raus != null ) { raus.println(ea); } else { sc.println(ea); } aus++; } while ( !ea.equals("ende") ); } catch (IOException e) { e.printStackTrace(); } finally { if ( raus != null ) raus.flush(); } try { rein.close(); if ( raus != null ) raus.close(); } catch (IOException e) { e.printStackTrace(); } sc.println(""+ein+" Zeilen von " + args[0] + " gelesen"); sc.print(""+aus+" Zeilen"); if ( args.length >= 2 ) sc.print(" auf "+args[1]); sc.println(" geschrieben"); } }
Benutzung:
java URLs http://www.uni-mannheim.de/ [Datei]
Die Klasse System
stellt eine
Reihe von Variablen und Klassen-Methoden
zum plattform-unabhängigen Zugriff auf das Computersystem
zur Verfügung.
public final class System extends Object PrintStream err InputStream in PrintStream out void setErr(PrintStream) void setIn(InputStream) void setOut(PrintStream) void arraycopy(Object, int, Object, int, int len) long currentTimeMillis() // Zeit seit 1. Jan 1970 void exit(int) void gc() Properties getProperties() String getProperty(String) String getProperty(String, String) void setProperties(Properties) SecurityManager getSecurityManager() void setSecurityManager(SecurityManager)
System UML Diagramm
import java.io.ObjectInputStream; import java.io.FileInputStream; import java.io.IOException; public class ObjectEin extends ObjectInputStream { public ObjectEin(String name) throws IOException { super(new FileInputStream(name)); } }
import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class ObjectAus extends ObjectOutputStream { public ObjectAus(String name) throws IOException { super(new FileOutputStream(name)); } }
import java.io.IOException; import java.io.FileNotFoundException; import java.io.OptionalDataException; public class Objekte { public static void main(String[] args) { Screen sc = new Screen(); KeyBoard kb = new KeyBoard(); final int MAX = 100; String[] data = null; if ( args.length < 1 ) { sc.println("Usage: Objekte <speicher datei>"); return; } ObjectEin rein = null; ObjectAus raus; try { rein = new ObjectEin(args[0]); } catch (FileNotFoundException e) { sc.println("Die Datei " + args[0] + " existiert nicht."); } catch (IOException e) { sc.println("Die Datei " + args[0] + " nicht lesbar."); } if ( rein != null ) { try { data = (String[]) rein.readObject(); rein.close(); } catch (OptionalDataException e) { sc.println("Zuviel Daten in Datei " + args[0]); } catch (ClassNotFoundException e) { sc.println("Zu lesende Klasse nicht bekannt in Datei " + args[0]+ ": "+e); } catch (IOException e) { sc.println("Lesefehler von Datei " + args[0]); } } if ( data == null ) { data = new String[MAX]; } try { raus = new ObjectAus(args[0]); } catch (IOException e) { sc.println("Die Datei " + args[0] + " ist nicht beschreibbar."); return; } String ea = ""; int zeile = -1; // leer int ein = 0; int aus = 0; sc.println("Aktueller Inhalt von " + args[0]); for ( zeile = 0; zeile < MAX; zeile++ ) { if ( data[zeile] == null ) break; sc.println("" + zeile + ": " + data[zeile] ); } // zeile zeigt jetzt auf einen freien Platz in data zeile--; sc.println(""+(zeile+1)+" Zeilen in " + args[0] + " enthalten\n"); try { sc.println( "Beenden mit 'ende'"); do { sc.print( "Bitte um Eingabe: "); sc.flush(); ea = kb.readLine(); if ( ea == null ) break; if ( ea.equals("ende") ) break; ein++; sc.println("Eingabe: "+(zeile+1)+": " +ea); zeile++; if ( zeile < MAX ) data[zeile] = ea; aus++; } while (true); } catch (IOException e) { e.printStackTrace(); } finally { try { raus.writeObject(data); raus.close(); } catch (IOException e) { sc.println("Fehler beim Schreiben auf Die Datei " + args[0]); } } sc.println(); sc.println(""+ein+" Zeile(n) von der Tastatur gelesen"); sc.println(""+aus+" Zeile(n) auf den Bildschirm geschrieben"); sc.println(""+(zeile+1)+" Zeile(n) nach " + args[0] + " geschrieben"); } }
Benutzung:
java Objekte xx Aktueller Inhalt von xx 0: a 1: b 2: c 3: ddddddddddddddd 4: e 5: f 6 Zeilen in xx enthalten Beenden mit 'ende' Bitte um Eingabe: 1 Eingabe: 6: 1 Bitte um Eingabe: 2 Eingabe: 7: 2 Bitte um Eingabe: 3 Eingabe: 8: 3 Bitte um Eingabe: 3 Zeile(n) von der Tastatur gelesen 3 Zeile(n) auf den Bildschirm geschrieben 9 Zeile(n) nach xx geschrieben
Dauerhafte nicht flüchtige Objekte
Speichern der Objekte auf externen Datenträgern
import java.io.IOException; import java.io.FileNotFoundException; import java.io.OptionalDataException; public class PersObjekt { public final int MAX = 100; private String[] data = null; private String fileName = "PersObjekt.temp"; private ObjectEin rein = null; private ObjectAus raus; private Screen sc = new Screen(); public PersObjekt(String filename) throws IOException { if ( filename != null ) if ( !filename.equals("") ) fileName = filename; restore(); if ( data == null ) { data = new String[MAX]; } raus = new ObjectAus(fileName); } protected void restore() throws IOException { try { rein = new ObjectEin(fileName); } catch (FileNotFoundException e) { throw new IOException("Die Datei " + fileName + " existiert nicht " + e); } if ( rein != null ) { try { data = (String[]) rein.readObject(); rein.close(); } catch (OptionalDataException e) { throw new IOException("Zuviel Daten in Datei " + fileName + e); } catch (ClassNotFoundException e) { throw new IOException("Zu lesende Klasse nicht bekannt in Datei " + fileName + e); } } } protected void store() throws IOException { raus.writeObject(data); raus.close(); sc.println(fileName + " geschrieben"); } public String[] getData() { return data; } public void setData(String[] d) { data = d; } protected void finalize() throws IOException { store(); } }
import java.io.IOException; import java.io.FileNotFoundException; import java.io.OptionalDataException; public class Persistenz { public static void main(String[] args) throws IOException { Screen sc = new Screen(); KeyBoard kb = new KeyBoard(); PersObjekt pers = null; String pname = "PersObjekt.temp"; if ( args.length > 0 ) { pname = args[0]; } pers = new PersObjekt(pname); String[] data = pers.getData(); String ea = ""; int zeile = -1; // leer int ein = 0; int aus = 0; sc.println("Aktueller Inhalt von " + pname); for ( zeile = 0; zeile < pers.MAX; zeile++ ) { if ( data[zeile] == null ) break; sc.println("" + zeile + ": " + data[zeile] ); } // zeile zeigt jetzt auf einen freien Platz in data zeile--; sc.println(""+(zeile+1)+" Zeilen in " + pname + " enthalten\n"); try { sc.println( "Beenden mit 'ende'"); do { sc.print( "Bitte um Eingabe: "); sc.flush(); ea = kb.readLine(); if ( ea == null ) break; if ( ea.equals("ende") ) break; ein++; sc.println("Eingabe: "+(zeile+1)+": " +ea); zeile++; if ( zeile < pers.MAX ) data[zeile] = ea; aus++; } while (true); } catch (IOException e) { e.printStackTrace(); } finally { pers.setData(data); // unnötig pers.store(); // pers = null; // System.gc(); // call finalize } sc.println(); sc.println(""+ein+" Zeile(n) von der Tastatur gelesen"); sc.println(""+aus+" Zeile(n) auf den Bildschirm geschrieben"); } }
Benutzung:
java Persistenz xx Aktueller Inhalt von xx 0: a 1: b 2: c 3: ddddddddddddddd 4: e 5: f 6: 1 7: 2 8: 3 9 Zeilen in xx enthalten Beenden mit 'ende' Bitte um Eingabe: I Eingabe: 9: I Bitte um Eingabe: II Eingabe: 10: II Bitte um Eingabe: III Eingabe: 11: III Bitte um Eingabe: IV Eingabe: 12: IV Bitte um Eingabe: ende xx geschrieben 4 Zeile(n) von der Tastatur gelesen 4 Zeile(n) auf den Bildschirm geschrieben
© Universität Mannheim, Rechenzentrum, 1998-2008.
Heinz KredelLast modified: Sun Oct 5 15:21:19 CEST 2008