2009年11月7日土曜日

Java 文字化け対策

環境:WindowsXP+JDK6+Tomcat5.5
Java で Web サイトを作ると
~ が文字化けする。
これはコードポイントに問題があるようです。

したがって文字列を
replace('\uff5e', '\u301c')
すればうまくいきました。

2009年11月2日月曜日

Java から外部ファイルを起動してキーを送る

外部 exe ファイルの起動
(例)notepad の起動
Runtime.getRuntime().exec("notepad");
※C:/Program Files/~~~のようにフルパスを指定することもできる。


notepad を立ち上げてキーストローク a を送る場合、

java.awt.Robot;
java.awt.event.KeyEvent;
をインポートして

Runtime.getRuntime().exec("notepad");
Robot robot = new Robot();
robot.delay(3000);
robot.keyPress(KeyEvent.VK_A);

sendKey メソッドを作ってもいい。
参考サイト http://d.hatena.ne.jp/muimy/20061227/1167226067
(char はシングルクオーテーションで囲む)

Java で 年 月 を求める

年の取得
Calendar.getInstance().get(Calendar.YEAR);

月の取得
Calendar.getInstance().get(Calendar.MONTH) + 1;

日を取得
Calendar.getInstance().get(Calendar.DATE);

2009年7月6日月曜日

Web ページの Content-Type を取得

Web ページの
Content-Type の値を取得してみました。
Content-Type : meta タグの Content-Type 属性で指定する値には、
shift_jis や utf-8 などがあります。

方法は

URL url = new URL("http://~~~~");
String cs = url.openConnection().getContentType();
cs = cs.substring(cs.lastIndexOf("=") + 1);

あるいは

BufferedReader br = new BufferedReader(
                            new InputStreamReader(url.openStream()));
                    String str;
                    while ((str = br.readLine()) != null) {
                        if (str.contains("charset=")) {
                            System.out.println(
                                    str.substring(str.indexOf("charset=") + 8, str.indexOf('"', str.indexOf("charset=") + 8)));
                            break;
                        }    
                    }

2009年6月16日火曜日

カンマ区切りの文字列を扱う

カンマ区切りの文字列で
2つ目に区切られた語を取り出したい場合、
下記の2つでどの方法が早いか実験。
public static void main(String[] args) {
     long start, goal;
     String str = "漢字,ひらがな,カタカナ,ローマ字";
     start = System.currentTimeMillis();
     for (int i=0; i < 100000; i++){ func1(str);}
     goal = System.currentTimeMillis();
     System.out.println("func1: " + (goal - start));

     start = System.currentTimeMillis();
     for (int i=0; i < 100000; i++){ func2(str);}
     goal = System.currentTimeMillis();
     System.out.println("func1: " + (goal - start));

     }
     static String func1(String str) {
         int index = str.indexOf(",") + 1;
         return str.substring(index, str.indexOf(",", index));
     }
     static String func2(String str) {
         return str.split(",")[1];
     }
結果は
func1: 16
func1: 438

1行で書けても遅い。ケースバイケースで使い分け。

2009年6月15日月曜日

InetAddress

InetAddress のインスタンスを ip アドレスから作成。
バイトの範囲を超える場合はキャストする。

InetAddress add = InetAddress.getByAddress(
new byte[]{(byte)203, (byte)216, (byte)235, (byte)154});
System.out.println(add.getHostName());

もしくは
InetAddress add = InetAddress.getByName("203.216.235.154");

2009年6月13日土曜日

HashMap を Value 値でソート

HashMap を Value 値でソートしてみた。 (JDK6)

HashMap の作成
Map scores = new HashMap();

Key は String、 Value は数値から Integer オブジェクトを入れてある。

昇順でソートしてみると

List<Map.Entry> entries = new ArrayList<Map.Entry>(scores.entrySet());
Collections.sort(entries, new Comparator(){
    public int compare(Object o1, Object o2){
        Map.Entry e1 =(Map.Entry)o1;
        Map.Entry e2 =(Map.Entry)o2;
        return ((Integer)e1.getValue()).compareTo((Integer)e2.getValue());
    }
});

取り出しは

for (Map.Entry entry : entries) {
    // entry.getKey()entry.getValue() を使ってみた。
}