Übungsblatt 08 - Lösungsvorschlag

Werbung
Universität Augsburg, Institut für Informatik
Prof. Dr. Werner Kießling
M. Endres, A. Huhn, T. Preisinger
Sommersemester 2006
29. Juni. 2006
Lösungsblatt 8
Informatik II
Aufgabe 1: Dateivergleich
TextCompare.java
import java.io.*;
public class TextCompare {
public static void compare(File f1, File f2) {
Reader r1, r2;
// Zähler für Ausgabe
int countBytes = 0, countLines = 1, countCol = 1;
int read1 = 0, read2 = 0;
boolean wasNewLine = false;
try {
// Input-Streams für bessere Performance verpacken
r1 = new BufferedReader(new FileReader(f1));
r2 = new BufferedReader(new FileReader(f2));
while (read1 != -1 && read1 == read2) {
// immer wieder ein Zeichen aus beiden Dateien lesen
read1 = r1.read();
read2 = r2.read();
if (read1 == read2) {
countBytes++;
countCol++;
if (read1 == 13) { // 0x0D gelesen
// => Windows/Unix-Zeilenumbruch
wasNewLine = true;
countLines++;
countCol = 1;
} else if (read1 == 10 && wasNewLine) { // 0x0D 0x0A gelesen
// => Windows-Zeilenumbruch
// Zeilenzahl nicht erhöhen, da bereits 0x10 gelesen wurde
countCol = 1;
wasNewLine = false;
} else if (read1 == 10) { // 0x0D gelesen
// => Mac-Zeilenumbruch
countLines++;
countCol = 1;
}
}
}
if (read1 == -1 && read2 == -1)
// Programm korrekt beendet.
System.out.println("Die Dateien sind identisch.");
else
System.out.println("Unterschied entdeckt an Byte " + countBytes
+ " (Zeile " + countLines + ", Spalte " + countCol + ").");
} catch (IOException e) {
e.printStackTrace();
}
}
1
public static void main(String[] args) {
if (args.length < 2) {
throw new IllegalArgumentException("Aufruf: TextCompare <file1> <file2>");
}
compare(new File(args[0]), new File(args[1]));
}
}
Aufgabe 2: Komprimierung
Gzip.java
import java.io.*;
import java.util.zip.GZIPOutputStream;
public class Gzip {
public static void gzip(File input, File output) {
if (output.exists()) {
System.out.println("Kann Dateien nicht überschreiben!");
return;
}
InputStream in;
OutputStream out;
try {
in = new BufferedInputStream(new FileInputStream(input));
out = new GZIPOutputStream(new FileOutputStream(output));
byte[] buffer = new byte[4096];
int read = 0;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.close();
in.close();
long oldSize = input.length();
long newSize = output.length();
System.out.printf("Komprimierung erfolgt. %1$.1f%% gespart.", 100.0 - 100.0 * newS
} catch (IOException e) { e.printStackTrace(); }
}
public static void main(String[] args) {
if (args.length < 2)
throw new IllegalArgumentException("Gebrauch: Gzip <file1> <file2>");
gzip(new File(args[0]), new File(args[1]));
}
}
Gunzip.java
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Gunzip {
public static void gunzip(File input, File output) {
if (output.exists()) {
System.out.println("Kann Dateien nicht überschreiben!");
return;
}
InputStream in;
2
OutputStream out;
try {
in = new GZIPInputStream(new FileInputStream(input));
out = new BufferedOutputStream(new FileOutputStream(output));
byte[] buffer = new byte[4096];
int read = 0;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.close();
in.close();
long oldSize = input.length();
long newSize = output.length();
System.out.printf("Dekomprimierung erfolgt. Volumen um %1$.1f%% vergrößert.", 100.
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length < 2) {
throw new IllegalArgumentException("Gebrauch: Gunzip <file1> <file2>");
}
gunzip(new File(args[0]), new File(args[1]));
}
}
Aufgabe 3: Graphische Oberfläche
FirstGuiGridBag.java
import java.awt.*;
import javax.swing.*;
public class FirstGuiGridBag extends JFrame {
public FirstGuiGridBag() {
super("Eine erste GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridBagLayout());
contentPane.add(new JButton("Knopf 1"), new GridBagConstraints(
0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5,5,10,15), 0, 0));
contentPane.add(new JButton("Knopf 2"), new GridBagConstraints(
1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(5,0,10,5), 0, 0));
contentPane.add(new JButton("Knopf 3"), new GridBagConstraints(
0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0,5,5,15), 0, 0));
contentPane.add(new JButton("Knopf 4"), new GridBagConstraints(
1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0,0,5,5), 0, 0));
this.pack();
}
public static void main(String[] args) {
3
new FirstGuiGridBag().setVisible(true);
}
}
FirstGuiBox.java
import java.awt.*;
import javax.swing.*;
public class FirstGuiBox extends JFrame {
public FirstGuiBox() {
super("Eine erste GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
Container contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JPanel firstLine = new JPanel();
firstLine.setLayout(new BoxLayout(firstLine, BoxLayout.LINE_AXIS));
firstLine.add(Box.createHorizontalStrut(5));
firstLine.add(new JButton("Knopf 1"));
firstLine.add(Box.createHorizontalStrut(15));
firstLine.add(new JButton("Knopf 2"));
firstLine.add(Box.createHorizontalStrut(5));
JPanel secondLine = new JPanel();
secondLine.setLayout(new BoxLayout(secondLine, BoxLayout.LINE_AXIS));
secondLine.add(Box.createHorizontalStrut(5));
secondLine.add(new JButton("Knopf 3"));
secondLine.add(Box.createHorizontalStrut(15));
secondLine.add(new JButton("Knopf 4"));
secondLine.add(Box.createHorizontalStrut(5));
secondLine.add(Box.createHorizontalGlue());
contentPane.add(Box.createVerticalStrut(5));
contentPane.add(firstLine);
contentPane.add(Box.createVerticalStrut(5));
contentPane.add(secondLine);
contentPane.add(Box.createVerticalStrut(5));
this.pack();
}
public static void main(String[] args) {
new FirstGuiBox().setVisible(true);
}
}
4
Herunterladen