2012年1月31日火曜日

[JAVA]文字列の読み込みと分割

ファイルから文字列を一行ずつ読み込んで、TABで分割を行う。
例外等は略

FileReader fr;
BufferedReader br;

fr = new FileReader("Station_a");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
  //処理だよー
  String tmp[] = line.split("\t");
}


タブは\tで表す。

[JAVA]文字コードを指定したファイル書き込み

韓国語を扱うプロジェクトから、いつも通りにファイル書き出しを行ったら文字化けの嵐だったので。
EUC-8→Shift_JISで。
例外等は省略

PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                                  new FileOutputStream(file), "Shift_JIS")));

pw.write(String);


文字コード(Character Set)に指定する名前はここ参照
IANA Charset Registry

[JAVA]HTMLから表(table)だけ取り出したい

Webページから表状になっているデータを取ってくる必要があったので、Jericho氏のparserを。
例外等は省略


Source source = new Source(new URL(URL));
List tableList = source.getAllElements(HTMLElementName.TABLE);
   
for(Element table : tableList){
  if(tableList.indexOf(table) > 0){
    for(Element tbody : table.getChildElements()){
      if(table.getChildElements().indexOf(tbody) > 1){
        for(Element tr : tbody.getChildElements()){
          if(tbody.getChildElements().indexOf(tr) >= 0){//ここが問題
            for(Element td : tr.getChildElements()){
              if(tr.getChildElements().indexOf(td) > 0){
                //処理だよー
                System.out.println(td.getTextExtractor().toString());
              }
            }//td
          }
        }//tr
      }
    }//tbody
  }
}//table


何これ簡単
でも、最初から<td>辺りを取得すれば良かったかも?

sourceforge - Jericho HTML Parser

2012年1月27日金曜日

[JAVA]正規表現

String regex = "\\d+";  //ex. 数値
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(inStr);

while(matcher.find){
    /* 処理 */
}

2012年1月26日木曜日

[Eclipse]大文字/小文字 置換

小文字->大文字
Cmd(Ctrl) + Shift + x

大文字->小文字
Cmd(Ctrl) + Shift + y

知らなかったとです。

2012年1月20日金曜日

[VBA]シート上のグラフを全削除

シート上にある全てのグラフ(チャート)を削除したい。


Sub removeGraph()
  For Each sh In Worksheets
    sh.ChartObjects.Delete
  Next sh
End Sub


--追記--
上記では削除出来ていないような…
一見消えている様に見えるが、ChartObjectsのCountで見る限り消えるものと消えないものがあったりなかったり
[Excel2003]


とりあえず、これで凌ぐ
Sub removeGraph()
  For Each sh In Worksheets
    For Each chart In sh.ChartObjects
      chart.Delete
    Next chart
  Next sh
End Sub