テキストファイルの処理


  読み込み

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

  

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();

} }
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[javaSwing]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 19:07:25
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube