1 // FileEncoding.java
2 import java.io.IOException;
3 import java.io.PrintWriter;
4 import java.nio.charset.Charset;
5
6 /** 7 * FileEncoding zeigt den Umgang mit Character-Streams und CharSets. 8 * Beispielprogramm zur Programmiertechnik 1, Teil 6. 9 * @author H.Drachenfels 10 * @version 10.1.2019 11 */
12 public final class FileEncoding {
13 private FileEncoding() { }
14
15 /** 16 * main schreibt die Zeichen 17 * äöü߀½²√∑ 18 * in eine Datei. 19 * @param args leer oder Name eines character sets 20 * @throws IOException bei Ausgabefehlern 21 */
22 public static void main(String[] args) throws IOException {
23 String charsetName = Charset.defaultCharset().name();
24 if (args.length > 0) {
25 charsetName = args[0];
26 } else {
27 System.out.printf("Using charset \"%s\" out of:%n", charsetName);
28 for (Charset cs : Charset.availableCharsets().values()) {
29 System.out.println(cs.name());
30 for (String s : cs.aliases()) {
31 System.out.printf(" %s%n", s);
32 }
33 }
34 }
35
36 String umlaute = "\u00E4\u00F6\u00FC\u00DF";
37 String euro = "\u20AC";
38 String symbole = "\u00BD\u00B2\u221A\u2211";
39
40 String fileName = "charset-" + charsetName + ".txt";
41
42 PrintWriter pw = new PrintWriter(fileName, charsetName);
43 pw.println(umlaute + euro + symbole);
44 pw.close();
45 }
46 }
47