40. 書き込み
40. 書き込み
文字ストリーム
L:\lesson101\test.java
import java.io.*; class test { public static void main(String args[]) { try { FileWriter fp = new FileWriter(args[0]); fp.write(args[1]); fp.close(); } catch (IOException e) { System.out.println("例外 - " + e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("使い方 - java test <ファイル名> <書き込む文字列>"); } } }
実行結果
L:\lesson101>java test test.txt testL:\lesson101>type test.txt
test
エンコーディング名
L:\lesson102\test.java
import java.io.*; class test { public static void main(String args[]) { try { FileWriter fp = new FileWriter(args[0]); System.out.println(fp.getEncoding()); fp.close(); } catch (IOException e) { System.out.println("例外 - " + e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("使い方 - java test <ファイル名>"); } } }
実行結果
L:\lesson102>java test test.txt
MS932
リダイレクト
L:\lesson103\test.java
import java.io.*; class test { public static void main(String args[]) { try { FileWriter fp = new FileWriter(FileDescriptor.out); fp.write("Kitty on your lap\n"); fp.flush(); fp = new FileWriter(FileDescriptor.err); fp.write("Back To Your True Shape"); fp.close(); } catch (IOException e) { System.out.println("例外 - " + e); } } }
実行結果
L:\lesson103>java test > out.txt 2> err.txtL:\lesson103>type *.txt
err.txt
Back To Your True Shape
out.txt
Kitty on your lap