Newer
Older
sampleNWP / src / j1 / ReadFile.java
@Motoki Miura Motoki Miura on 22 Sep 2020 950 bytes first commit for NWP exp
package j1;

// File: ReadFile.java
// ファイルの内容を読み取り,そのまま画面に出力する
// 使い方: java Readfile <ファイル名>
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFile {
    public static void main(String[] args){
	byte[] buff = new byte[1024];
	FileInputStream infile = null;
		
	try{
	    infile = new FileInputStream(args[0]) ;
	}
	catch(FileNotFoundException e){	// ファイル準備の失敗
			
	    System.err.println("Error: File not found.") ;
	    System.exit(1) ;
	}

	while (true) {
	    try {
		int n = infile.read(buff);
		System.out.write(buff, 0, n) ;
	    }
	    catch(Exception e){	// 読み出し終了
		break;		// 繰り返しを終了
	    }
	}

	try{
	    infile.close() ;
	}
	catch(IOException e){
	    System.err.println("Error(close).") ;
	    System.exit(1) ;
	}
    }
}