Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Praktikum Entwicklung und Einsatz von Geosoftware I - Sitzung 9 IO – Streams in Java Sommersemester 2003 Lars Bernard.

Ähnliche Präsentationen


Präsentation zum Thema: "Praktikum Entwicklung und Einsatz von Geosoftware I - Sitzung 9 IO – Streams in Java Sommersemester 2003 Lars Bernard."—  Präsentation transkript:

1 Praktikum Entwicklung und Einsatz von Geosoftware I - Sitzung 9 IO – Streams in Java Sommersemester 2003 Lars Bernard

2 Geosoftware I – Lars Bernard Überblick Lesen und Speichern in Dateien: Filestreams und Tokenizer Kurzeinführung in Exceptions und das Exception-Handling in Java

3 Geosoftware I – Lars Bernard Streams Daten werden sequentiell aus einem Daten-Strom gelesen, bzw. in einen Daten-Strom geschrieben Ein solcher Datenstrom kann zu unterschiedlichen Quellen (Datei, Speicher, Netzressource) und Datenformate (Binär, Text, …) verbunden sein – zeigt aber immer dasselbe Verhalten

4 Geosoftware I – Lars Bernard Lesen und Schreiben in Streams Reading open a stream while more information read information close the stream Writing open a stream while more information write information close the stream Das Prinzip ist immer gleich:

5 Geosoftware I – Lars Bernard 2 Arten von Streams in Java Character-Streams Byte-Streams Mehr Infos im Sun-Tutorial unter: Essential Java Classes Lesson: I/O: Reading and Writing (but no 'rithmetic)

6 Geosoftware I – Lars Bernard Copy Beispiel aus dem Tutorial import java.io.*; public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); FileReader in = new FileReader(inputFile); // file treated as char stream FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); }

7 Geosoftware I – Lars Bernard Beispiel Punkte schreiben/lesen Einfaches ARC-Generate Ascii-Dateiformat für Punkte: Mehr Infos unter: http://gis.washington.edu/cfr250/lessons/data_export/

8 Geosoftware I – Lars Bernard Beispiel Punkte schreiben - 1 import java.util.*; import java.io.*; class GeneratePointFileWriter { void write(String fileName, java.util.List pointList) throws IOException { PrintWriter out = new PrintWriter(new FileWriter(new File(fileName))); double x, y; Point p; int i = 1; java.util.Iterator pointIterator = pointList.iterator(); while (pointIterator.hasNext()) { p = (Point)pointIterator.next(); x = p.getX(); y = p.getY(); out.println(i + ", " + x + ", " + y); i++; } out.println("END"); out.close(); }

9 Geosoftware I – Lars Bernard Beispiel Punkte schreiben - 2 … final JFileChooser fileChooser = new JFileChooser(); int returnVal = fileChooser.showSaveDialog(GISFrame.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); GeneratePointFileWriter pointWriter = new GeneratePointFileWriter(); try { pointWriter.write(file.getAbsolutePath(), model.getPointLayer() ); } catch (Exception IOException) { // writing failed: JOptionPane.showMessageDialog(GISFrame.this, "Can not store File:" + file.getAbsolutePath(), "Writing Error", JOptionPane.ERROR_MESSAGE); } }…

10 Geosoftware I – Lars Bernard Beispiel Punkte lesen - 1 public class GeneratePointFileReader { public java.util.List read(String name) throws IOException { BufferedReader in = new BufferedReader(new FileReader(name)); java.util.List pointList = new ArrayList(); double x, y; int i = 0; // read first line String line = in.readLine(); while (!line.equals("END")) { // using StringTokenizer to parse string; using comma as delimeter StringTokenizer st = new StringTokenizer(line, ", ", false); st.nextToken(); //overread point-number // extract the point x = Double.valueOf(st.nextToken()).doubleValue(); y = Double.valueOf(st.nextToken()).doubleValue(); Point p = new Point(x, y); pointList.add(p); // read next line line = in.readLine(); } in.close(); return pointList; }

11 Geosoftware I – Lars Bernard Beispiel Punkte lesen - 2 … final JFileChooser fileChooser = new JFileChooser(); int returnVal = fileChooser.showOpenDialog(GISFrame.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); GeneratePointFileReader pointReader = new GeneratePointFileReader(); try { newPoints = pointReader.read(file.getAbsolutePath()); } catch (Exception IOException) { // reading failed: JOptionPane.showMessageDialog(GISFrame.this, "Can not read File:" + file.getAbsolutePath(), "Reading Error", JOptionPane.ERROR_MESSAGE); } …

12 Geosoftware I – Lars Bernard StringTokenizer Dient der Zerlegung einer Zeichenkette (in einem String oder Stream) in Teilzeichenketten Voreingestellte Trennzeichen sind Leerzeichen, Tabulatoren und Zeilenvorschübe Es können andere Trennzeichen definiert werden Mehr Infos: www.javabuch.de; Kapitel 17.1 www.javabuch.de

13 Geosoftware I – Lars Bernard Beispiel StringTokenizer while (!line.equals("END")) { // using StringTokenizer to parse // string; using comma as delimeter StringTokenizer st = new StringTokenizer(line, ", ", false); st.nextToken(); //overread point-number // extract the point x = Double.valueOf(st.nextToken()).doubleValue(); y = Double.valueOf(st.nextToken()).doubleValue(); Point p = new Point(x, y); pointList.add(p); // read next line line = in.readLine(); }

14 Geosoftware I – Lars Bernard Überblick Exceptions in Java Eine Exception ist ein außergewöhnliches Verhalten, dass den normalen (= geplanten) Programmfluss unterbricht (Lese- oder Schreibefehler, Division durch Null, …) Eine Exception wird dort geworfen (throw), wo der Fehler auftritt und dort aufgefangen (try…catch), wo der Fehler behandelt werden soll Exceptions müssen aufgefangen werden ! Mehr Infos im Sun-Tutorial unter: Essential Java Classes Lesson: Handling Errors with Exceptions

15 Geosoftware I – Lars Bernard Exceptions in Java – try & catch Der try catch Block: try {... } catch (... ) {... } catch (... ) {... }...

16 Geosoftware I – Lars Bernard Exceptions in Java – try & catch Unterschiedliche Spezialisierung von der Basisklasse Exception für unterschiedliche Behandlungen: try {... } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }

17 Geosoftware I – Lars Bernard Exceptions in Java – try & catch In dem Beispiel zum einlesen von Punkten: try { pointWriter.write(file.getAbsolutePath(), model.getPointLayer() ); } catch (Exception IOException) { // writing failed: JOptionPane.showMessageDialog(GISFrame.this, "Can not store File:" + file.getAbsolutePath(), "Writing Error", JOptionPane.ERROR_MESSAGE); }

18 Geosoftware I – Lars Bernard Eine Methode wirft eine Exception weiter: void write(String fileName, java.util.List pointList) throws IOException { BufferedReader in = new BufferedReader(new FileReader(name));... } Eine Methode erzeugt und wirft eine Exception: public Object pop() throws EmptyStackException { Object obj; if (size == 0) throw new EmptyStackException(); // EmptyStackException is a self created class // that is derived from Exception... } Exceptions in Java – throw

19 Geosoftware I – Lars Bernard Aufgabe 7 Erweiterung des Programms so, dass mindestens Punkt-Layer und Polylinien-Layer unter verwendung des ARC ASCII Generate Format (neu) eingelesen und abgespeichert werden können Die angegebenen Stellen in dem Tutorial lesen und erarbeiten ! Der Code soll mittels Java Doc dokumentiert werden – selbständig im Tutorial einarbeiten! Fragen dazu in der nächsten Sitzung. Abgabe: Montag 21.07. 12.00 !

20 Geosoftware I – Lars Bernard Aufgabe – Format für Liniendateien Das ARC ASCII Generate Format zur Abspeicherung von Polylinien Jede Linie beginnt mit einer ID und endet mit END Die Datei endet mit END

21 Geosoftware I – Lars Bernard Aufgabe – Format für Polygondateien Das ARC ASCII Generate Format zur Abspeicherung von Polygonen Jedes Polygon beginnt mit einer ID sowie einem Labelpoint und endet mit END Die Datei endet mit END

22 Geosoftware I – Lars Bernard Vorschlag für ein Klassendiagramm


Herunterladen ppt "Praktikum Entwicklung und Einsatz von Geosoftware I - Sitzung 9 IO – Streams in Java Sommersemester 2003 Lars Bernard."

Ähnliche Präsentationen


Google-Anzeigen