テキストファイルの処理

  読み込み



以下はオーソドックスなテキストファイルを読み込む例です

  
String filePath = "D:\\TEMP\\query.txt";
String line = null;

try {

	BufferedReader inFile = new BufferedReader( new FileReader( filePath ) );

	while ( ( line = inFile.readLine() ) != null) {
		System.out.println( line );
	}

	inFile.close();

}
catch ( Exception e ) {
	System.out.println( e.getMessage() );
}
  



  open 処理を内部メソッド化



myOpen は、static で定義しているので、クラスメソッドとして内部で直接呼ぶ事ができます
また、例外処理をメソッド内で行なっているので try ブロック内 で書く必要がありません

  
public static void main(String[] args) {

	String filePath = "D:\\TEMP\\query.txt";
	String line = null;
	BufferedReader inFile = null;

	inFile = myOpen( filePath );
	if ( inFile == null ) {
		System.out.println( filePath + " を open できません" );
		return;
	}

	try {
		while ( ( line = inFile.readLine() ) != null) {
			System.out.println( line );
		}

		inFile.close();

	}
	catch ( Exception e ) {
		System.out.println( e.getMessage() );
	}

}

private static BufferedReader myOpen( String filePath ) {

	BufferedReader inFile = null;

	try {
		inFile = new BufferedReader( new FileReader( filePath ) );
	}
	catch ( Exception e ) {
		return null;
	}

	return inFile;
}
  



  内部 static クラス

前述のメソッドをクラスに入れて、クラス経由で呼び出します

  
public static void main(String[] args) {

	String filePath = "D:\\TEMP\\query.txt";
	String line = null;
	BufferedReader inFile = null;

	inFile = myFile.myOpen( filePath );
	if ( inFile == null ) {
		System.out.println( filePath + " を open できません" );
		return;
	}

	try {
		while ( ( line = inFile.readLine() ) != null) {
			System.out.println( line );
		}

		inFile.close();

	}
	catch ( Exception e ) {
		System.out.println( e.getMessage() );
	}

}


private static class myFile {
	public static BufferedReader myOpen( String filePath ) {

		BufferedReader inFile = null;

		try {
			inFile = new BufferedReader( new FileReader( filePath ) );
		}
		catch ( Exception e ) {
			return null;
		}

		return inFile;
	}
}
  



  外部クラスの static メソッド

クラスを外部にしてメソッドを static にし、いわゆる クラスメソッド として実行します

  
public class CLCOMP {

	public static void main(String[] args) {

		String filePath = "D:\\TEMP\\query.txt";
		String line = null;
		BufferedReader inFile = null;

		inFile = myFile.myOpen( filePath );
		if ( inFile == null ) {
			System.out.println( filePath + " を open できません" );
			return;
		}

		try {
			while ( ( line = inFile.readLine() ) != null) {
				System.out.println( line );
			}

			inFile.close();

		}
		catch ( Exception e ) {
			System.out.println( e.getMessage() );
		}

	}

}

class myFile {
	static public BufferedReader myOpen( String filePath ) {

		BufferedReader inFile = null;

		try {
			inFile = new BufferedReader( new FileReader( filePath ) );
		}
		catch ( Exception e ) {
			return null;
		}

		return inFile;
	}
}

  



  外部クラスのインスタンスメソッド

このメソッドの性質上、インスタンスメソッドにする理由はありませんが、( オブジェクト毎に保有すべきデータが無い )
メソッドから static を外す事によって呼び出し側はインスタンスを作成する必要が出て来ます

  
public class CLCOMP {

	public static void main(String[] args) {

		String filePath = "D:\\TEMP\\query.txt";
		String line = null;
		BufferedReader inFile = null;

		myFile myObj = new myFile();

		inFile = myObj.myOpen( filePath );
		if ( inFile == null ) {
			System.out.println( filePath + " を open できません" );
			return;
		}

		try {
			while ( ( line = inFile.readLine() ) != null) {
				System.out.println( line );
			}

			inFile.close();

		}
		catch ( Exception e ) {
			System.out.println( e.getMessage() );
		}

	}

}

class myFile {
	public BufferedReader myOpen( String filePath ) {

		BufferedReader inFile = null;

		try {
			inFile = new BufferedReader( new FileReader( filePath ) );
		}
		catch ( Exception e ) {
			return null;
		}

		return inFile;
	}
}
  



  テキストファイル読み込みクラス

BufferedReader の1 行のテキスト読み込み部分をラップして、c ライクにしたものです。
読み込みデータを呼び出し側で用意して、そこにデータセットさせる為に String の配列を使用しています。

  
public class CLCOMP {

	public static void main(String[] args) {

		String filePath = "D:\\TEMP\\query.txt";
		String line [] = {""};

		myFile myObj = new myFile();

		if ( !myObj.open( filePath ) ) {
			System.out.println( filePath + " を open できません" );
			return;
		}

		while ( myObj.get( line ) ) {
			System.out.println( line[0] );
		}

		myObj.close();

	}

}

class myFile {

	private BufferedReader inFile = null;

	public boolean open( String filePath ) {

		try {
			inFile = new BufferedReader( new FileReader( filePath ) );
		}
		catch ( Exception e ) {
			return false;
		}

		return true;
	}
	public boolean get( String buff [] ) {

		String line;

		try {
			line = inFile.readLine(); 
			if ( line == null ) {
				return false;
			}
			buff[0] = line;
		}
		catch ( Exception e ) {
			return false;
		}

		return true;
	}
	public void close( ) {

		try {
			inFile.close();
		}
		catch ( Exception e ) {
		}

		return;
	}
}
  



  トークン

  
import java.io.*;
import java.util.*;

public class CLCOMP { public static void main(String[] args) {

	String filePath = "商品分類マスタ.csv";
	String line [] = {""};

	myFile myObj = new myFile();

	if ( !myObj.open( filePath ) ) {
		System.out.println( filePath + " を open できません" );
		return;
	}

	StringTokenizer st = null;

	while ( myObj.get( line ) ) {
		st = new StringTokenizer( line[0],"," );
		while ( st.hasMoreTokens() ) {
			System.out.print( st.nextToken() );
			if ( st.hasMoreTokens() ) {
				System.out.print("|");
			}
		}
		System.out.println("");
	}

	myObj.close();

} }
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ