Java関連

Javaのリフレクション

Java文法系Tips

Eclipse関連

JavaのWebアプリ開発

Tips集Webサイト

一般

Java並行処理

クラスロード時に実行されるコード

public class testmain {
	
	static {
		try {
			Runtime r = Runtime.getRuntime();
			Process p = r.exec("C:\\WINDOWS\\system32\\notepad.exe");
			System.out.println("実行した");
			p.waitFor();
			System.out.println("メモ帳閉じた");
			p.destroy();
			System.out.println("destroyed");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("The main function is called.");
	}
}

JavaのSerializableを実際にアプリケーションを動かしながら理解する

util.Objects

日付/時刻処理いろいろ

Runtime#addShutdownHookで JVM 終了時に処理を実行する

Velocity

マルチスレッド

強制ガベージコレクション

テキストファイルの読み込み

イメージをpngファイルに出力

 /**
  * pngファイルに出力
  * @param im 出力したいイメージ
  * @param fname 出力ファイル名
  * @throws FileNotFoundException
  * @throws IOException
  */
 public static void savePng(BufferedImage im, String fname)
     throws FileNotFoundException, IOException {
   // Open output file.
   OutputStream out = new FileOutputStream(fname);
   
   ImageOutputStream ios =
           ImageIO.createImageOutputStream(out);
   ImageWriter iw = (ImageWriter)ImageIO.
           getImageWritersByFormatName("png").next();
   iw.setOutput(ios);
   iw.write(im);
   
   // Close output file.
   out.close();
 }

イメージ⇔バイト列の変換

 /**
  * イメージをバイト列に変換する
  * @param im イメージ
  * @param formatName フォーマット名 png, bmp, jpeg など
  * @return バイト列
  * @throws IOException
  */
 public static byte[] getBytesFromImage(BufferedImage im, String formatName) 
   throws IOException{
   
   if(formatName == null)
     formatName = "png";
   
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   ImageIO.write(im, formatName, bos);
   return bos.toByteArray();
 }
 
 /**
  * バイト列からイメージを作成する
  * @param bytes
  * @return イメージ
  * @throws IOException
  */
 public static BufferedImage getImageFromBytes(byte[] bytes) 
         throws IOException{
   
   ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
   BufferedImage im = ImageIO.read(bis);
   return im;
 }

フォントにアンチエイリアスをかける

BigDecimalの丸め方

指定のバイト長さになるようにスペースを詰める

  * @param s 対象の文字列
  * @param length つめた結果の長さ(バイト長)
  * @param leftPad trueなら左に詰める(文字は右詰になる)
  * @return 詰めた文字列
  * @throws UnsupportedEncodingException 
  */
 public static String padSpaceHankaku(String s, int length, booleanleftPad) 
           throwsUnsupportedEncodingException{
   if(isEmpty(s)){
     return repeatChar(' ', length);
   }
   else {
     //シフトJISでバイトにしたときの長さを得て、足りない分を補う
     byte[] bytes = s.getBytes("SJIS");
     int spaceLen = length - bytes.length; 
     if(spaceLen <= 0)
       return s;
     else {
       if(leftPad)
         return repeatChar(' ',spaceLen) + s;
       else
         return s + repeatChar(' ',spaceLen);
     }
   }
 }

バイト列の16進文字列化

private String toHex(byte buffer[]) {
    StringBuffer sb = new StringBuffer(buffer.length * 2);
    for (int i = 0; i < buffer.length; i++) {
         sb.append(Character.forDigit((buffer[i] & 0xf0) >> 4, 16));
         sb.append(Character.forDigit(buffer[i] & 0x0f, 16));
    }
    return sb.toString();
}

jar内のクラスファイルの一覧を得るバッチファイル

前ゼロつける

ファイル一覧取得

Java のスタンドアロンプログラムで終了コードを設定するには

System.exit(-1);

PDFを生成する

XMLに対応したProperties

プロパティファイル読み込み

スタンドアロンアプリで二重起動防止

ImageI/O関連

Graphics2D+ImageI/Oを使って画像に別の画像を挿入したファイルを作成する

正規表現によるマッチング

import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
public boolean isHogeNumFormat(String arg) {
  String regexp = <正規表現の文字列>;
  Pattern pat = Pattern.compile(regexp);
  return pat.matcher(arg).matches();
}

シリアルバージョンUIDの生成

実行時の対応ソース行取得

int line = (new Throwable().getStackTrace()[0]).getLineNumber();

Javaオブジェクトのハッシュコードのルール

コンパイラの警告抑制

バイト配列から文字列への変換

result = new String(bytes, offset, length, charsetName);

JNDIのlookupをだましてとりあえず動かす

 public class DummyNamingContext implements InitialContextFactoryBuilder, InitialContextFactory, Context {
	public static DummyNamingContext setup() throws Exception {
	  if (!NamingManager.hasInitialContextFactoryBuilder()) {
	    final DummyNamingContext instance = new DummyNamingContext();
	    NamingManager.setInitialContextFactoryBuilder(instance);
	    return instance;
	  }
	  final Context con = NamingManager.getInitialContext(null);
	  if (con instanceof DummyNamingContext) {
	    return (DummyNamingContext) con;
	  }
	  throw new IllegalStateException("No DummyNamingContext");
	}
   private final Map<Name, Object> nameMap;
   private final Map<String, Object> strMap;
 
	@Override
   public void bind(final String name, final Object obj) throws NamingException {
     this.strMap.put(name, obj);
   }
	
	@Override
	public Object lookup(final String name) throws NamingException {
		return this.strMap.get(name)
	}
 
	...
 }

Javaのソース解析/AST(Abstract Syntax Tree)

その他


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS