##################################### # Javaアプリのさんぷる (jdk1.0) # ##################################### ファイル操作(入出力)のための,以下の2点 が含まれています. (個々に独立しています) ・read_file.java ファイル内容を読み込んで, コンソール上に表示するプログラム ・write_in.java ファイルへ文字列を書き込む プログラム(新規にtmp.txtと いう名のファイルが作られます) プログラムは初心者でもすぐ分かる ように,極力単純化してあります. アプリケーションですので,jdkをインストール後, javac read_file.java のようにコマンド入力してコンパイルし,できた クラスファイル(拡張子: class)を java read_file のようにコマンド入力して実行して下さい. Presented by Koide Tetsuya. These are free soft programs. < Date: 2000.12.8 > 以下に、ソースコード(S-JIS CR+LF)を公開します。→ダウンロード ################ read_file.java ######################### // Display a paticular file's contents. import java.io.*; // for using a "FileInputStream" etc. class read_file{ public static void main(String args[]){ String fname="read_file.java"; // * file name to read String line_str; try{ FileInputStream fis=new FileInputStream(fname); DataInputStream dis=new DataInputStream(fis); while((line_str=dis.readLine())!=null){ // read line System.out.println(line_str); // output } dis.close(); // close a stream }catch(IOException e){ // if read-error occur ...below System.out.println("#### Not such a file !! ####"); } } } ################# write_in.java ########################## // Write data in a paticular file. import java.io.*; // for using a "FileOutputStream" etc. class write_in{ public static void main(String args[]){ String fname="tmp.txt"; // * file name to write in String line_str; try{ FileOutputStream fos=new FileOutputStream(fname); PrintStream ps=new PrintStream(fos); ps.println("It's easy, isn't it ?"); // write a message in a file ps.println("Enjoy my Java world !"); // write a message, too ps.close(); // close a stream }catch(IOException e){ // if write-error occur ...below System.out.println("#### file write-error occured !! ####"); } } }