図 文字ストリーム
文字ストリームクラス | 解説 | 対応するバイトクラス |
---|---|---|
Reader | 文字入力ストリームの抽象クラス | InputStream |
BufferedReader | 入力をバッファに格納し、行を構文解析する | BufferedInputStream |
LineNumberReader | 行番号を追跡する | LineNumberInputStream |
CharArrayReader | 文字配列から読み込む | ByteArrayInputStream |
InputStreamReader | バイトストリームを文字ストリームに変換する | (なし) |
FileReader | ファイルのバイトを文字ストリームに変換する | FileInputStream |
FilterReader | フィルタをかけられた文字入力の抽象クラス | FilterInputStream |
PushbackReader | 文字をプッシュバックする | PushbackInputStream |
PipedReader | PipedWriter から読み込む | PipedInputStream |
StringReader | String から読み込む | StringBufferInputStream |
Writer | 文字出力ストリームの抽象クラス | OutputStream |
BufferedWriter | 出力をバッファに格納し、プラットフォームの行区切り文字を使用する | BufferedOutputStream |
CharArrayWriter | 文字配列に書き込む | ByteArrayOutputStream |
FilterWriter | フィルタをかけられた文字出力の抽象クラス | FilterOutputStream |
OutputStreamWriter | 文字ストリームをバイトストリームに変換する | (なし) |
FileWriter | 文字ストリームをバイトファイルに変換する | FileOutputStream |
PrintWriter | 値およびオブジェクトを Writer に出力する | PrintStream |
PipedWriter | PipedReader に書き込む | PipedOutputStream |
StringWriter | String に書き込む | (なし) |
入力ストリーム | 出力ストリーム | |
---|---|---|
クラス | Reader FileReader BufferedReader |
Writer FileWriter BufferedWriter |
コンストラクター | BufferedReader( new FileReader(String name)) |
BufferedWriter(new FileWriter(String name)) BufferedWriter(new FileWriter(String name, boolean append)) |
入出力メソッド | int read() String readLine() |
void write(int c) void write(String s) void newLine() void flush() |
使用例 | while ((c = fr.read()) != -1) { ... } while ((s = br.readLine()) != null) { ... } |
fw.write(c); bw.write(s); bw.newLine(); bw.flush(); |
クローズ | void close() | void close() |
- import java.io.*;
- public class FileCopy {
- public static void main(String[] args) {
- if (copyFile(args[0], args[1])) {
- System.err.println("File '" + args[0] + "' copied to '" + args[1] + "'.");
- } else {
- System.err.println("File '" + args[0] + "' does not copied.");
- }
- }
- static boolean copyFile(String inFileName, String outFileName) {
- try {
- // 文字ストリームの獲得(ファイルのオープン)
- BufferedReader br = new BufferedReader(new FileReader(inFileName));
- BufferedWriter bw = new BufferedWriter(new FileWriter(outFileName));
- String str;
- // データの読み込み(入力)
- while ((str = br.readLine()) != null) {
- // 読み込んだデータの書き込み(出力)
- bw.write(str);
- bw.newLine();
- }
- // ストリームのクローズ(ファイルのクローズ)
- bw.flush();
- bw.close();
- br.close();
- } catch (FileNotFoundException e) {
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
□ 実行結果
$ java FileCopy FileCopy.java a.txt File 'FileCopy.java' copied to 'a.txt'.
- import java.io.*;
- public class FileCopy {
- public static void main(String[] args) {
- String inf = "", outf = "";
- String ie = // コードを記述してください。
- String oe = // コードを記述してください。
- for (int i = 0; i < args.length; i++) {
- if (args[i].equals("-ie")) {
- ie = args[++i];
- } else if (args[i].equals("-oe")) {
- oe = args[++i];
- } else {
- if (inf.equals("")) {
- inf = args[i];
- } else {
- outf = args[i];
- }
- }
- }
- if (copyFile(ie, oe, inf, outf)) {
- System.err.println("File '" + inf + "(" + ie
- + ")' copied to '" + outf + "(" + oe + ")'.");
- } else {
- System.err.println("File '" + inf + "' does not copied.");
- }
- }
- static boolean copyFile(String inFileEncoding,
- String outFileEncoding,
- String inFileName,
- String outFileName) {
- try {
- // 文字ストリームの獲得(ファイルのオープン)
- BufferedReader br =
- // コードを記述してください。
- BufferedWriter bw =
- // コードを記述してください。
- String str;
- // データの読み込み(入力)
- while ((str = br.readLine()) != null) {
- // 読み込んだデータの書き込み(出力)
- bw.write(str);
- bw.newLine();
- }
- // ストリームのクローズ(ファイルのクローズ)
- bw.flush();
- bw.close();
- br.close();
- } catch (FileNotFoundException e) {
- return false;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
□ 実行結果
$ java FileCopy FileCopy.java a.txt File 'Stream.dat(JISAutoDetect)' copied to 'aaa.dat(MS932)'. $ java FileCopy -ie MS932 Stream.dat aaa.dat File 'Stream.dat(JISAutoDetect)' copied to 'aaa.dat(MS932)'.
- import java.io.*;
- public class FileCopy {
- public static void main(String[] args) {
- // 前問と同じ。
- if (outf.equals("")) {
- if (encodeFile(ie, oe, inf)) {
- System.err.println("File '" + inf + "(" + ie
- + ")' encoded to '" + oe + "'.");
- } else {
- System.err.println("File '" + inf
- + "' does not encorded.");
- }
- } else {
- if (copyFile(ie, oe, inf, outf)) {
- // 前問と同じ。
- }
- }
- }
- static boolean encodeFile(String inFileEncoding,
- String outFileEncoding,
- String fileName) {
- String tmpFileName = fileName + ".tmp";
- // コードを記述してください。
- return deleteFile(tmpFileName);
- }
- static boolean deleteFile(String fileName) {
- // コードを記述してください。
- }
- static boolean copyFile(String inFileEncoding,
- String outFileEncoding,
- String inFileName,
- String outFileName) {
- // 前問と同じ。
- }
- }
□ 実行結果
$ java FileCopy -ie EUC_JP aaaa.txt File 'aaaa.txt(EUC_JP)' encoded to 'MS932'.
$ javac -d . FileCopy.java $ jar -cf maruno.jar maruno/*.class $ cp maruno.jar $JAVA_HOME/jre/lib/ext/
□ 実行結果
$ java -cp $JAVA_HOME/jre/lib/ext/maruno.jar maruno.FileCopy -ie EUC_JP aaaa.txt File 'aaaa.txt(EUC_JP)' encoded to 'MS932'.
type \t code \t name \t priceまた、出力ファイルの形式は以下のとおりです。
code \t name \t price
- // 1601
- // package server;
- // import product.*;
- import java.util.*;
- import java.io.*;
- // セット商品クラスをファイルによって管理するクラスです。
- public class AdminProduct {
- // ファイルから商品データを読んで、セット商品クラスに商品を設定します。
- public void read(SetProduct sp, String fileName) {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(fileName));
- String s = null;
- while ((s = br.readLine()) != null) {
- String[] sa = s.split("\t");
- if (sa.length >= 2 && sa[0].toUpperCase().equals("D")) {
- sp.remove(sa[1]);
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("A")) {
- sp.add(new Product(sa[1], sa[2], sa[3]));
- } else if (sa.length >= 4 && sa[0].toUpperCase().equals("U")) {
- sp.update(new Product(sa[1], sa[2], sa[3]));
- } else {
- continue;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // ファイルにセット商品データの商品を設定します。
- public void write(SetProduct sp, String fileName) {
- Map p = (Map)sp.getProducts();
- BufferedWriter bw = null;
- try {
- bw = new BufferedWriter(new FileWriter(fileName));
- Product product = null;
- Iterator itr = p.values().iterator();
- while (itr.hasNext()) {
- product = (Product)itr.next();
- bw.write(product.getCode() + "\t" + product.getName() + "\t" + product.getPrice());
- bw.newLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- bw.flush();
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // 1601
- // package application;
- // import product.*;
- import java.util.*;
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- TreeMap p = new TreeMap();
- SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
- System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
- System.out.println();
- AdminProduct ap = new AdminProduct();
- ap.read(s, args[0]);
- System.out.println("商品のデータを入力しました。 p = " + p);
- System.out.println("セット商品のデータを設定しました。 s = " + s);
- ap.write(s, args[1]);
- System.out.println("セット商品のデータをファイルに書き出しました。 s = " + s);
- }
- }
A A001 パソコン 188000 A P001 プリンター 24000 A C001 CD-ROM 6500 A M001 USBメモリー 9800 A H001 ハードディスク 9200 U C001 DVD-ROM 9200 D M001
□ 実行結果
$ java -cp classes application.TestProduct products.txt out.txt セット商品のインスタンスを生成しました。 s = [SA01, パソコン・セット, 10, 0] 商品のデータを入力しました。 p = {A001=[A001, パソコン, 188000], C001=[C001, DVD -ROM, 9200], H001=[H001, ハードディスク, 9200], P001=[P001, プリンター, 24000]} セット商品のデータを設定しました。 s = [SA01, パソコン・セット, 10, 207360] セット商品のデータをファイルに書き出しました。 s = [SA01, パソコン・セット, 10, 207360]
A001 パソコン 188000 C001 DVD-ROM 9200 H001 ハードディスク 9200 P001 プリンター 24000
"EUC_JP" EUC "SJIS" Shift JIS "ISO2022JP" ISO