テキストファイルを扱うクラス

テキストファイルを読み書きするクラスです。
別になくてもいいのだけど、あったほうがちょっとだけ楽になります。
よく使うクラスです。


ファイルに書き込むときは、
outOpen("*****.***");でファイルを作り、
tf.writeN( "***" );か、
tf.write( "***" );でファイルに文章を書いて
tf.outClose();で閉じて終了。


ファイルを読み込むときは、 tf.inOpen("test.txt");で既存のファイルを作り、
String s = tf.readAll();か
s = tf.read();でファイルの中身を読み込んで、
tf.inClose();でファイルを閉じる



以下 プログラム ソース


package tomojavalib.util;

import java.io.*;



/**
* textファイルを入出力するclass
* @author to.totomo.net
* 履歴:
* 01/04/21製作開始
* 01/08/16 ライブラリtomolib.utilに移す
* 20090520 ライブラリtomojavalib.utilに移し再構成する
*/
public class TextFile {
   
protected FileOutputStream fill = null;
protected PrintStream p = null;
protected BufferedReader br = null;
   
   /**
    * このClassの動作確認用.
    *
    */
   public static void main(String[] args) {
      tomojavalib.util.TextFile tf = new tomojavalib.util.TextFile();
   try{
   System.out.println("test");
   tf.outOpen("test.txt");
   tf.writeN( "試験書き込み改行なし_" );
   tf.write( "試験書き込み改行つき-" );
   tf.write( "2行目" );   
   tf.outClose();
   }catch(Exception e){   System.out.println( "a" );};try{
   tf.inOpen("test.txt");
   String s = tf.readAll();
   System.out.println( s );
   s = tf.read();
   System.out.println( s );
   tf.inClose();
   }catch(Exception e){};
   }
   
   /**
   * 読み込みで開いているファイルから1行読み込む
   * なければString"null"を返す
   *@return 1行の文字列
   */  
   public String read() throws Exception
   {
    String line = null;
    try {
      line = br.readLine();    // 1行単位で入力
    }catch(Exception ex ) { throw new Exception("文字が読めません");    };
   if(line == null){line = "null";}
   return line;
   }
   
   
   /**
   * ファイルを読み込みのために開く
   * @param filename file名
   * @throws
   */  
   public void inOpen( String filename ) throws Exception
   {
    try{
    br = new BufferedReader( new FileReader( filename ) );
    }catch(Exception ex){    throw new Exception("読み込みファイルが開けません");    };
    return ;
   }
   
   
   /**
   * 読み込みで開いたファイルを閉じる
   */  
  public void inClose() throws Exception
   {
    try {
    br.close();
    }catch( Exception e ){    throw new Exception("読み込みファイルが開けません");    };
  return;
   }
   
   /**
   * 書き込みで開いたファイルを閉じる
   */  
   public void outClose() throws Exception
   {
    try {
    p.close();
    }catch( Exception e ){    throw new Exception("読み込みファイルが開けません");    };
    return ;
   }
   
   /**
   * ファイルを書き込みのために開く
   * 書き込むファイルは上書きされるので注意が必要
   * @param filename ファイル名
   * 20090520再構成
   */  
   public void outOpen( String filename )throws Exception
   {
    try{
    fill = new FileOutputStream(filename);
    }catch(Exception ex){    throw new Exception("書き込みファイルが開けません");    };
    p = new PrintStream (fill);
   }   
   
   
   /**
   * ファイルの中身全部読み込む
   * ファイルがない、ファイルの中身がない場合はString"null"を返す
   * @return テキストファイルの中身の文字全部
   * @author to.totomo.net
   * 20020903作成20090520再構成
   */  
   public String readAll() throws Exception
   {
    byte stb[] = new byte[10000];
    byte stb2[] = null;
    byte tmpb;
    int i;
   try {
    for(i=0;i<10000;i++)
    {
    tmpb = (byte)( br.read() );
    if( tmpb == -1 ){break;}
    stb[i] = tmpb;
    }
    stb2 = new byte[i];
    for( int ii=0;ii    }catch(Exception ex ) {throw new Exception("ファイルの中身を読めませんでした");}
   String line = new String( stb2 );
   if(line == null){return "null";}
   return line;
   }

   
   /**
   * テキストファイルに1行書き込み.最後に改行が入る
   * @param s fileに追加するData
   */  
   public void write(String s) throws Exception
   {
      try{
        p.println(s);
      }catch(Exception ex ) {throw new Exception("書き込めませんでした");}
   }
   
   /**
   * テキストファイルに1行書き込み.最後に改行をつけない
   * @param s fileに追加するData
   */  
   public void writeN(String s) throws Exception
   {
      try{
        p.print(s);
      }catch(Exception ex ) {throw new Exception("書き込めませんでした");}
   }   
   
}

作成日: 2009-06-02 00:00:00

テキストファイルを扱うクラス

  • ともさん
  • ソーイング
  • ものつくり
  • みつばち
  • 気象
  • 電子工作
  • デジタルカメラ
  • ともさんの開発室
  • プロブラミング
  • ものつくり
  • リホーム
  • 食べ物作り
  • JAVALibrary
  • 1
  • このページは「ともさん」が個人的に運営しています。

    RSS配信しています

    Yahoo!ブックマークに登録

    My Yahoo!に追加

    - リンク -
    ともさんブログ
    おデブとふつうの分水嶺 そんなに食うなら走らないと

    Contact me
    da

    null