メモ、備忘録、その他雑記を記載します。
ただし、このHPに記載している情報を利用した結果 損失・損害等が発生したとしても筆者は責任を持ちません。
Stringはint型等と同じ扱いで、値渡しはできるが参照渡しできない。
参照渡ししたい場合はStringBufferを使用する。
import java.io.*;
/**
* メインクラス
* @author
* @version 1.0
*/
public class Main
{
/**
* アプリケーション起動処理
* @param args コマンドライン引数
* @return なし
*/
public static void main( String[] args )
{
System.out.println("Hello");
String str = "123";
System.out.println( str );
Func1( str );
System.out.println( str );
System.out.println();
StringBuffer sb = new StringBuffer( "123" );
System.out.println( sb );
Func2( sb );
System.out.println( sb );
System.out.println();
}
public static void Func1( String str )
{
str = "456";
// これは値渡しでコピーしたstrを変更しているだけ
// ×
}
public static void Func2( StringBuffer sb )
{
sb.append( "456" );
// 参照渡ししたオブジェクトに対して文字列操作
// ○
}
}
参照渡ししたい場合はStringBufferを使用する。
import java.io.*;
/**
* メインクラス
* @author
* @version 1.0
*/
public class Main
{
/**
* アプリケーション起動処理
* @param args コマンドライン引数
* @return なし
*/
public static void main( String[] args )
{
System.out.println("Hello");
String str = "123";
System.out.println( str );
Func1( str );
System.out.println( str );
System.out.println();
StringBuffer sb = new StringBuffer( "123" );
System.out.println( sb );
Func2( sb );
System.out.println( sb );
System.out.println();
}
public static void Func1( String str )
{
str = "456";
// これは値渡しでコピーしたstrを変更しているだけ
// ×
}
public static void Func2( StringBuffer sb )
{
sb.append( "456" );
// 参照渡ししたオブジェクトに対して文字列操作
// ○
}
}
PR
ファイルをバイナリ形式で読み込む処理の雛形
import java.io.*;
System.out.println("Hello");
try
{
int n;
FileInputStream is = new FileInputStream("test.txt");
while ( true )
{
n = is.read();
if ( n == -1 )
{
break;
}
System.out.print(Integer.toHexString( n ) + " ");
}
is.close();
}
catch ( IOException e )
{
System.out.println( e );
}
import java.io.*;
System.out.println("Hello");
try
{
int n;
FileInputStream is = new FileInputStream("test.txt");
while ( true )
{
n = is.read();
if ( n == -1 )
{
break;
}
System.out.print(Integer.toHexString( n ) + " ");
}
is.close();
}
catch ( IOException e )
{
System.out.println( e );
}
テキストファイルを1行毎に読み込む処理の雛形
/**
* ファイル読込処理
* @param strFile ファイル名
* @return なし
*/
public static void ReadFile( String strFile )
{
String strLine;
try
{
// ファイル存在確認
File file = new File( strFile );
if( file.exists() == false )
{
return;
}
// ファイルオープン
FileReader fr = new FileReader( strFile );
BufferedReader br = new BufferedReader( fr );
// ファイル1行読込ループ
for ( ;; )
{
// BufferedReaderで1行読込
strLine = br.readLine();
// 終了判定
if ( strLine == null )
{
break;
}
// 任意の処理
}
// 終了化処理
if ( br != null )
{
br.close();
br = null;
}
if ( fr != null )
{
fr.close();
fr = null;
}
}
catch ( Exception ex )
{
ex.printStackTrace();
System.err.println( ex );
}
return;
}
/**
* ファイル読込処理
* @param strFile ファイル名
* @return なし
*/
public static void ReadFile( String strFile )
{
String strLine;
try
{
// ファイル存在確認
File file = new File( strFile );
if( file.exists() == false )
{
return;
}
// ファイルオープン
FileReader fr = new FileReader( strFile );
BufferedReader br = new BufferedReader( fr );
// ファイル1行読込ループ
for ( ;; )
{
// BufferedReaderで1行読込
strLine = br.readLine();
// 終了判定
if ( strLine == null )
{
break;
}
// 任意の処理
}
// 終了化処理
if ( br != null )
{
br.close();
br = null;
}
if ( fr != null )
{
fr.close();
fr = null;
}
}
catch ( Exception ex )
{
ex.printStackTrace();
System.err.println( ex );
}
return;
}