Initial commit

This commit is contained in:
github-classroom[bot]
2022-04-28 08:39:46 +00:00
commit 2a34df16f4
47 changed files with 6848 additions and 0 deletions
@@ -0,0 +1,16 @@
a
B
c
d
f
ü
_
+50
View File
@@ -0,0 +1,50 @@
/*
* Gradle build configuration for specific lab module / exercise
*/
// enabled plugins
plugins {
id 'java'
// Apply the application plugin to add support for building a CLI application.
id 'application'
}
// Project/Module information
description = 'Lab05 UnderstandingCharsets Solution'
group = 'ch.zhaw.prog2'
version = '2022.1'
// Dependency configuration
repositories {
mavenCentral()
}
dependencies {
}
// Configuration for Application plugin
application {
// Define the main class for the application.
mainClass = 'ch.zhaw.prog2.io.UnderstandingCharsets'
}
// enable console input when running with gradle
run {
standardInput = System.in
}
// Java plugin configuration
java {
// By default the Java version of the gradle process is used as source/target version.
// This can be overridden, to ensure a specific version. Enable only if required.
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
// Java compiler specific options
compileJava {
// source files should be UTF-8 encoded
options.encoding = 'UTF-8'
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
}
}
@@ -0,0 +1,94 @@
package ch.zhaw.prog2.io;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class UnderstandingCharsets {
public static void main(String[] args) {
/* Teilaufgabe a
* In der Vorlesung haben Sie gelernt, dass Java-Klassen fuer Unicode entworfen wurden.
* Nun ist Unicode aber nicht der einzige Zeichensatz und Java unterstuetz durchaus Alternativen.
* Welche Zeichensaetze auf einem System konkret unterstuetzt werden haengt von der Konfiguration des Betriebssystems JVM ab.
* Schreiben Sie ein Programm, welches alle Unterstuetzten Zeichensaetze auf der Konsole (System.out) ausgibt,
* zusammen mit dem Standardzeichensatz.
* https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html
*/
// Print default character set
System.out.println("Default Charset = " + Charset.defaultCharset());
// Print all available character sets
System.out.println("Available Charsets:");
for (Charset charset : Charset.availableCharsets().values()) {
System.out.println("- " + charset);
}
/* Ende Teilaufgabe a */
/* Teilaufgabe b
* Schreiben Sie ein Program welches im Standardzeichensatz einzele Zeichen (also Zeichen fuer Zeichen) von der
* Konsole einliest und ebenso im Zeichensatz US_ASCII in eine Datei schreibt.
* Die Eingabe des Zeichens 'q' soll das Program ordentlich beenden.
* Die Datei soll "CharSetEvaluation.txt" genannt werden und wird entweder erzeugt oder wenn Sie bereits
* existiert, einfach geoeffnet und der Inhalt uebeschrieben werden.
* Lesen von der Konsole und Schreiben in die Datei soll leistungsoptimiert geschehen, also vom jeweiligen
* Input-/Output-Medium entkoppelt.
* Testen Sie Ihr Program mit den folgenden Eingabereihenfolge und Zeichen: a B c d € f _ q
* Oeffnen Sie die Textdatei nach Durchfuehrung des Programs mit einem Texteditor und erklaeren Sie das Ergebnis.
* Oeffnen Sie die Datei anschliessend mit einem HEX-Editor und vergleichen Sie.
*/
char c;
try (FileOutputStream fosDefault = new FileOutputStream("CharSetEvaluation_Default.txt");
FileOutputStream fosAscii = new FileOutputStream("CharSetEvaluation_ASCII.txt");
FileOutputStream fosUTF8 = new FileOutputStream("CharSetEvaluation_UTF8.txt");
FileOutputStream fosWin = new FileOutputStream("CharSetEvaluation_WIN1252.txt");
BufferedWriter bwdefault = new BufferedWriter(new OutputStreamWriter(fosDefault));
BufferedWriter bwascii = new BufferedWriter(new OutputStreamWriter(fosAscii, StandardCharsets.US_ASCII));
BufferedWriter bwutf8 = new BufferedWriter(new OutputStreamWriter(fosUTF8, StandardCharsets.UTF_8));
BufferedWriter bwwin = new BufferedWriter(new OutputStreamWriter(fosWin, Charset.forName("windows-1252")));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
{
System.out.println("Enter characters, 'q' to quit."); // read characters
boolean reading = true;
while (reading) {
c = (char) br.read();
/* returns character read, as an integer (32bit) in the range 0 to 65535
(0x00-0xffff, 16bit), or -1 (0xffff-ffff) if the end of the stream has been reached */
if (c == '\n') {
continue;
}
if (c == 'q') {
System.out.println("The End");
reading = false;
} else {
System.out.println("== Output using Default Encoding");
bwdefault.write(c);
bwdefault.newLine();
bwdefault.flush();
System.out.println("== Output using ASCII Encoding");
bwascii.write(c);
bwascii.newLine();
bwascii.flush();
System.out.println("== Output using UTF-8 Encoding");
bwutf8.write(c);
bwutf8.newLine();
bwutf8.flush();
System.out.println("== Output using Windows-1252 Encoding");
bwwin.write(c);
bwwin.newLine();
bwwin.flush();
}
//int dummy = br.read(); //clear CRNL
}
} catch (IOException e) {
System.err.println("Abbruch wegen IO-Exception" + e.getMessage());
e.printStackTrace();
}
}
}