package j1;
// WriteFile.java
// キーボードからの入力を受け取り,そのまま画面に出力する.
// また,ファイルに入力を順に格納する.
// 使い方java Writefile ファイル名
// プログラムを終了するには,行の先頭にピリオド.を入力する.
import java.io.*;
public class WriteFile {
public static void main(String[] args){
byte[] buff = new byte[1024];
boolean cont = true ;
FileOutputStream outfile = null; // ファイル出力用オブジェクト
try{
outfile = new FileOutputStream(args[0]) ;
}
catch(FileNotFoundException e){ // ファイル準備の失敗
System.err.println("File not found") ;
System.exit(1) ;
}
// 行頭でピリオドが入力されるまで繰り返す.
while (cont) {
try {
int n = System.in.read(buff);
System.out.write(buff, 0, n) ;
if(buff[0] == '.') cont = false ;
else outfile.write(buff,0,n) ;
}
catch(Exception e){
System.exit(1); // プログラムの終了
}
}
try{
outfile.close() ;
}
catch(IOException e){
System.err.println("Error in closing a file") ;
System.exit(1) ;
}
}
}