プロ2 > 資料 > 第8章

第8回:ストリーム入出力と日本語の扱い

目標

8.1 ストリーム入出力

import java.util.*; public class Sample82a { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //標準入力のためのScannerを生成 while (true) { try { System.out.print("x> "); //プロンプトを表示する String s =sc.nextLine(); //1行読み込む Double x = Double.parseDouble(s); //実数に変換する System.out.print("y> "); //プロンプトを表示する s =sc.nextLine(); //1行読み込む Double y = Double.parseDouble(s); //実数に変換する System.out.printf("x + y = %.10f\n", x + y); //x+yを表示する System.out.print("continue? "); //"continue?"を表示する s = sc.nextLine(); if (!s.matches("^[yY]")) { //入力がyまたはYであるかを判定する break; } } catch (Exception ex) { System.out.println("? " + ex.toString()); //エラー表示をする } } sc.close(); //Scannerを閉じる } }

8.2 ファイルやネットワークの入出力

import java.io.File; import java.io.PrintStream; public class FileWrite { public static void main(String[] args) { PrintStream pr = null; try { pr = new PrintStream(new File("test.txt")); // "test.txt"というファイルを開く pr.println("2020.6.30 1000 食料品 生活費"); // ファイルに書き込む pr.println("2020.6.30 1500 文房具 学習費"); // ファイルに書き込む pr.close(); // ファイルを閉じる } catch (Exception ex) { System.err.println(ex); // エラー表示をする } } }

8.3 日本語の扱いと文字コード

8.4 例題:家計簿管理プログラム

import java.util.*; import java.io.*; public class Sample83 { static ArrayList<String> date = new ArrayList<String>(); // 日付を保存するArrayList static ArrayList<Integer> amt = new ArrayList<Integer>(); // 金額を保存するArrayList static ArrayList<String> item = new ArrayList<String>(); // 支出内容を保存するArrayList static ArrayList<String> kind = new ArrayList<String>(); // 分類を保存するArrayList public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { try { System.out.print("command> "); // コマンドプロンプト String[] cmd = sc.nextLine().split("[ \t]+"); // 空白かタブで文字列を分割する if (cmd.length == 0) { // コマンドがなければ何もしない // do nothing } else if (cmd[0].equals("exit")) { // コマンドが"exit"なら終了 sc.close(); System.exit(0); } else if (cmd[0].equals("read")) { // コマンドが"read"なら if (cmd.length < 2) { // コマンド引数にファイル名がなければエラー表示する System.out.println("Usage: read <filename>"); continue; } read(cmd[1]); // ファイルを読み込む } else if (cmd[0].equals("print")) { // コマンドが"print"なら PrintStream pr = System.out; // 出力をコンソールにする if (cmd.length > 1) { // 出力をファイルにする pr = new PrintStream(new File(cmd[1]), "UTF-8"); print(pr); pr.close(); } else print(pr); // データを表示する } else if (cmd[0].equals("sum")) { // コマンドが"sum"なら PrintStream pr = System.out; // 出力をコンソールにする if (cmd.length > 1) { // 出力をファイルにする pr = new PrintStream(new File(cmd[1]), "UTF-8"); sum(pr); pr.close(); } else sum(pr); // 合計値を表示する } else { // 正しくないコマンドの場合はエラー表示する System.out.println("Unknown command: " + cmd[0]); } } catch (Exception ex) { System.out.println("? " + ex.toString()); } } } public static void read(String f) throws Exception { Scanner sc = new Scanner(new File(f), "UTF-8"); // ファイルをオープンする int lineno = 0; // 行番号を0にする System.out.println(f); while (sc.hasNextLine()) { // ファイルの最後まで繰り返す String line = sc.nextLine(); // 一行読み込む System.out.println(line); ++lineno; // 行番号を1増やす String[] a = line.split("[ \t]+"); // 入力を空白またはタブで分割する try { String d = a[0], i = a[2], k = a[3]; int m = Integer.parseInt(a[1]); // 2列目のデータを整数化する date.add(d); // 日付をArrayListに加える amt.add(m); // 金額をArrayListに加える item.add(i); // 支出内容をArrayListに加える kind.add(k); // 分類をArrayListに加える } catch (Exception ex) { System.out.printf("line %d: spurious line: '%s'\n", lineno, line); } } sc.close(); } public static void print(PrintStream pr) throws Exception { for (int i = 0; i < date.size(); ++i) { // ArrayListの内容を表示する pr.printf("%-8s %8d %s %s\n", date.get(i), amt.get(i), item.get(i), kind.get(i)); } } public static void sum(PrintStream pr) throws Exception { TreeMap<String, Integer> tbl = new TreeMap<String, Integer>(); // TreeMap tblを用意する for (int i = 0; i < date.size(); ++i) { // ArrayListの要素分だけ繰り返す int a = amt.get(i); // i番目の要素の金額をaに代入する int t = 0; if (tbl.containsKey("TOTAL")) { // tblに"TOTAL"という鍵がふくまれていればtにその値を加える t += tbl.get("TOTAL"); } tbl.put("TOTAL", t + a); // tblに鍵"TOTAL"と値t+aを代入する String k = kind.get(i); // i番目の要素の分類をkに代入する t = 0; if (tbl.containsKey(k)) { // tblにkという鍵がふくまれていればtにその値を加える t += tbl.get(k); } tbl.put(k, t + a); // tblに鍵kと値t+aを代入する } for (String k : tbl.keySet()) { // TreeMap tblの内容を表示する pr.format("%s = %d\n", k, tbl.get(k)); } } }

付録:正規表現



ykitamura@kwansei.ac.jp