Java/TIPS集
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
#topicpath
----
あ、これってべんりだなあって思ったコーディングのメモ。
#contents
** AWS のLinuxでJavaを使う [#h783f118]
ふつうにwgetでrpm取ってきて、あとはJavaの切り替え。
wget --no-check-certificate --no-cookies --header \
"Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f...
sudo rpm -Uvh jdk-8u161-linux-x64.rpm
sudo alternatives --config java
export JAVA_HOME=/usr/java/jdk1.8.0_161
ちなみにダウンロード先はこちら
http://www.oracle.com/technetwork/java/javase/downloads/i...
** HttpURLConnection での接続において、Proxyを越える(SSL...
Jersey などライブラリを使ってる場合はこちら。。
[[JavaでSSLでRESTを投げるときに、プロキシを通す方法 - Qii...
ピュア(?)のHttpURLConnection を使ってるときはこんな感じで...
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketA...
final String authUser = "user";
final String authPassword = "pass";
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentic...
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
HttpsURLConnection.setDefaultSSLSocketFactory( createSSL...
// setup a hostname verifier that verifies everything
HttpsURLConnection.setDefaultHostnameVerifier(createHost...
HttpsURLConnection con = (HttpsURLConnection)url.openCon...
// HttpURLConnection con = (HttpURLConnection)url...
createSSLContext()、createHostNameVerifier() メソッドは、...
** JavaでAWSのプロファイルを切り替える件 [#kcffb36a]
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.stan...
.withRegion(Regions.AP_NORTHEAST_1)
.withCredentials(new ProfileCredentialsProvider(...
.build();
こんな感じ。
参考:
http://www.masatom.in/pukiwiki/AWS/AWS%20CLI/#w1297a6a
** Mavenから AWS SDK for Java を使う場合 [#p24dc007]
-[[Apache Maven での SDK の使用 - AWS SDK for Java>https:...
**Jerseyのエラー [#dd13ed6b]
org.glassfish.jersey.message.internal.MessageBodyProvide...
MessageBodyWriter not found for media type=application/...
type=class java.util.HashMap, genericType=class java.uti...
ってエラーが出る場合。pom.xmlやクラスパスに、
<!-- jersyでJavaBeansをJSON でPOSTする場合。 -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
これが足りないから、かも!
**よく出てくるコマンド。 [#f2fc0494]
Eclipseの設定。
mvn -DdownloadSources=true -DdownloadJavadocs=true eclip...
Eclipse上からデバッグできるようにする
export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket...
SpringBootの起動
mvn spring-boot:run
Jettyの起動
mvn jetty:run
ファイルをつくる。
$ cat << __EOF__ >> output.txt
> aaa
> bbb
> ccc
> __EOF__
$ cat output.txt
aaa
bbb
ccc
もはやJava関係ないな。。
**AWSのLinuxで Mavenを使う [#sb1c834a]
AWSのLinuxで、速攻でMavenでビルドしたくなって、しらべた内...
-[[Mavenをyumからインストール - Qiita>http://qiita.com/di...
-[[Amazon LinuxでJava8/Tomcat8の環境を構築する | Develop...
**Logの制御 [#pff20c16]
かつては Log4j がただ唯一の選択肢でしたが、どうやら乱立し...
-[[SLF4J+Lobackの基本 | Java好き>http://javazuki.com/arti...
SLF4J経由でLogback。ついでにLombokでAnnotation。これで決...
** 設定ファイルの制御 [#k95a870d]
ResourceBundleを使った設定ファイルの制御について。Resourc...
下記のサンプルは設定情報をクラスパス上のプロパティファイ...
package nu.mine.kino.resourcesamples;
import java.util.Enumeration;
import java.util.ResourceBundle;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Main {
public static void main(String[] args) {
new Main().execute();
}
// sample.properties をクラスパスから探して、なかっ...
// デフォルト値は、nu.mine.kino.resourcesamples.Defa...
private void execute() {
String propertyFile = "sample";
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle(propertyFi...
doSettings(bundle);
} catch (java.util.MissingResourceException e) {
String message = "設定ファイルが存在しません...
log.warn(message, propertyFile, e.getMessage...
doSettings(ResourceBundle.getBundle(
"nu.mine.kino.resourcesamples.Defaul...
}
}
private void doSettings(ResourceBundle bundle) {
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
log.debug("key[{}]:{}", key, bundle.getStrin...
}
}
}
package nu.mine.kino.resourcesamples;
import java.util.ListResourceBundle;
public class DefaultResources extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] { { "param1", "default val...
{ "param2", "default value2" } };
}
}
sample.properties
param1=property value1
param2=property value2
** ファイルパスの操作(Java7版) [#md2ed45b]
ファイルのパスをつなげるようなときに、もちろんStringでつ...
File base = new File("/tmp/base");
new File(new File(base, "more1"), "more2");
こんな風にくるんでくるんで、みたいな処理をしていたのが、
Paths.get("/tmp/base","more1","more2").toFile();
このように可変長引数を使ってつなげるようになりました。い...
** バイナリファイルをbyte配列に変換する(Java7版) [#va30b8...
便利な世の中になりました。
try {
byte[] bytes = Files.readAllBytes(Paths.get("パス"));
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
自前で書く必要なくなっちゃいました。AutoCloseableで自動で...
**ある配列に配列を追加して新しい配列を作る [#vfc530c0]
Object[] newArray = ArrayUtils.addAll(array1, array2);
**ある配列に単一のObjectを追加して新しい配列を作る [#j9ab...
Object[] array = ...;
Object element = ...;
Object[] newArray = ArrayUtils.add(array, element);
**Commonsのデリミタで分割するヤツ [#y2ba4cd9]
String[] splits = StringUtils.splitPreserveAllTokens(tar...
**StringBufferでカンマつなぎしたとき、最後のカンマはいら...
StringBuffer buf = new StringBuffer();
for (String str : array) {
buf.append(str);
buf.append(',');
}
buf.deleteCharAt(buf.length() - 1);
return new String(buf);
**わすれがちな、Dateの比較 before/after [#l032efcf]
Date base = new Date();
Date target = base;
System.out.println(base.after(target)); //おなじ日はfalse
System.out.println(base.before(target)); //おなじ日はfalse
System.out.println(base.before(DateUtils.addDays(target,...
System.out.println(base.after(DateUtils.addDays(target, ...
結果は
false
false
true
true
**バイト列を表示する。 [#j0b5477a]
テキストファイルが文字化けしてて、どんなバイナリの値なん...
private void printByte1(byte[] bytes) {
String hexString = toHexString(bytes);
System.out.println(hexString);
}
// byte[] -> 16進文字列へ変換
private String toHexString(byte[] b) {
StringBuffer hexString = new StringBuffer();
String plainText = null;
for (int i = 0; i < b.length; i++) {
plainText = Integer.toHexString(0xFF & b[i]);
if (plainText.length() < 2) {
plainText = "0" + plainText;
}
hexString.append(plainText);
hexString.append(" ");
}
return new String(hexString);
}
String#formatメソッドを使えば書式の指定で対応出来ますね。。
private void printByte2(byte[] bytes) {
for (byte b : bytes) {
System.out.printf(String.format("%1$x ", b));
}
System.out.println();
}
そもそもどうやってバイナリでファイル読むのっていうのは下...
**ファイルをバイナリで読み込む。。 [#t3df6b6b]
定番はコレですかね
String baseDirStr = "/tmp";
String fileName = "data.txt";
FileInputStream in = new FileInputStream(new File(baseDi...
int ch;
while ((ch = in.read()) != -1) {
// System.out.print(Integer.toHexString(ch) + " ");
System.out.printf(String.format("%1$x ", ch));
}
こういうのもある。。
RandomAccessFile accessFile = null;
try {
File file = new File(baseDirStr, fileName);
accessFile = new RandomAccessFile(file, "r");
int length = (int) file.length();
byte[] bytes = new byte[length];
accessFile.read(bytes);
printByte1(bytes);
printByte2(bytes);
} finally {
if (accessFile != null) {
accessFile.close();
}
}
RandomAccessFile にはバイナリファイルを readDouble とかや...
テキストファイルをバイナリで読んでみるときとかは前者が良...
**数値の文字列フォーマット変換 [#hb5bb0e4]
doubleとかの数値をフォーマッティングした文字列を取得する...
String.format("%1$,.0f" + " KB中、" + "%2$,.0f"+ " KB処...
a / 1024.0, b / 1024.0, c / 1024.0);
なかのプレースホルダは、それぞれ
%1$ -> a / 1024.0
%2$ -> b / 1024.0
%3$ -> c / 1024.0
って可変長の引数に対応。さらに
,.0f
で、「3ケタでカンマ区切り」「小数点以下は0ケタ」って意...
-[[数値の文字列フォーマット変換 ( Javaサンプル集 )>http:/...
-[[Java書式付き出力メモ(Hishidama's Java Formatter/Format...
**java.util.Date -> int [#t19069d3]
ついでに。。
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); <-2009/6/8に実行した
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year);
System.out.println(month);
System.out.println(day);
などで、Dateをintに変換可能です。
2009
5
8
Monthだけは日本人の感覚と一つ値がずれるので注意。
同様に、Calendar#set(int year, int month, int date) 、、...
Calendar calendar = Calendar.getInstance();
calendar.clear(); <-下で時分を指定しないから、clearしな...
calendar.set(year, month, day);
Date time = calendar.getTime();
とすることで、int -> java.util.DateもOKそうですね。
[[Calendar (Java 2 Platform SE 5.0)>http://java.sun.com/j...
**String -> java.util.Date [#w5cb107d]
文字列からjava.util.Dateを生成する方法ですが、
Date parseDate = new SimpleDateFormat("yyyy/MM/dd").pars...
System.out.println(parseDate);
などとやればできますが、Commons Langの org.apache.commons...
Date date = DateUtils.parseDate(dateStr, new String[] { ...
引数のパターンで順々にdataStrをパースして、Dateクラスを生...
**java.util.Date -> String [#i44fc664]
逆に、Dateから文字列を取得する方法。
String nowDateString = DateFormatUtils.format(nowDate, "...
System.out.println(nowDateString);
フォーマットの指定の仕方によって、年を取得、月を取得、日...
**「日」より下を切り捨てたDateを取得する [#z1f98e71]
日付だけ管理したいんだよなぁってときがよくありますが、Dat...
Date nowDate = new Date();
System.out.println("Today: " + nowDate);
// 「日」より下を切り捨て
Date dateTruncate = DateUtils.truncate(nowDate, Calendar...
System.out.println("Today: " + dateTruncate + " <-「日」...
// 「月」より下を切り捨て
dateTruncate = DateUtils.truncate(nowDate, Calendar.MONT...
System.out.println("Today: " + dateTruncate + " <-「月」...
// 「年」より下を切り捨て
dateTruncate = DateUtils.truncate(nowDate, Calendar.YEAR);
System.out.println("Today: " + dateTruncate + " <-「年」...
結果は以下の通り:
Today: Mon Jun 08 10:00:54 JST 2009
Today: Mon Jun 08 00:00:00 JST 2009 <-「日」より下を切り...
Today: Mon Jun 01 00:00:00 JST 2009 <-「月」より下を切り...
Today: Thu Jan 01 00:00:00 JST 2009 <-「年」より下を切り...
**stackTraceを取得する [#mcb50e85]
標準エラーに出力するならe.printStackTrace()ですが、文字列...
private String exception2String(Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream...
e.printStackTrace(new PrintStream(baos));
return baos.toString();
}
文字コードとか大丈夫だっけとかありますが、こんな感じでで...
**プロパティファイルから、複数のデータを配列で取得する [#...
[[Java Collections Framework>http://commons.apache.org/co...
-hoge.properties
data=hoge@fuga.com
data=hogehoge@fuga.jp
などという複数のデータを配列で取得することができます。こ...
上記データにアクセスするサンプルコードは以下の通り。
ExtendedProperties props = new ExtendedProperties();
ClassLoader loader = Thread.currentThread().getContextCl...
InputStream resourceAsStream = loader
.getResourceAsStream("hoge.properties");
InputStream in = new BufferedInputStream(resourceAsStrea...
props.load(in);
String[] dataArray = props.getStringArray("data");
などとしてデータを取り出すことができます。
-[[リソースへのアクセス>http://java.sun.com/j2se/1.5.0/ja...
-[[プロパティファイルから、String配列の値を取り込むには?...
-[[ExtendedPropertiesの自動読み込みについて - Java Soluti...
-[[Commons Collections>http://muimi.com/j/jakarta/commons...
**パイプされた標準入力を取得する [#uf4f1804]
そういえばJavaって標準入力をどう受け取るんだっけ??とい...
package nu.mine.kino.mail.utils;
import java.io.BufferedInputStream;
import java.io.IOException;
public class Echo {
public static void main(String[] args) {
BufferedInputStream in = new BufferedInputStream...
try {
int size = in.available();
byte[] bytes = new byte[size];
in.read(bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
}
結果はたとえば
[hogehoge@www Test]$ ls | java -cp lib/sample.jar nu.m...
build.win.properties
build_jar.xml
classes
lib
source
[hogehoge@www Test]$
lsの結果を受け取り、echoできました。
-シェルからJavaを呼び出すときもOK
[hogehoge@www Test]$ cat sample.sh
export JAVA_HOME=/opt/jdk1.5.0_12
export LIBDIR=lib
export CLASSPATH=${LIBDIR}/sample.jar
java nu.mine.kino.mail.utils.Echo
[hogehoge@www Test]$ ls | ./sample.sh
build.win.properties
build_jar.xml
classes
lib
sample.sh
source
[hogehoge@www Test]$
ちなみに標準入力を待ち受けてるときに入力完了を通知するた...
[[Platform-Specific Details: The End-of-Input Character>h...
参考
-[[標準入出力ストリーム>http://www.ei.fukui-nct.ac.jp/~yf...
**Apache Axis 1.4のSSL処理にはバグがありそう [#f0df163f]
詳しくはまた時間がとれたらにしますが、Basic認証するプロキ...
-[[Svn内のソース>http://svn.apache.org/viewvc/webservices...
**BigDecimalのコンストラクタにdoubleは使わないほうがいい ...
ちゃんと数値計算しようと思って、doubleとかの代わりにBigDe...
って、よく見たらJavaDocにも書いてありますね。
-[[BigDecimal>http://docs.oracle.com/javase/jp/6/api/java...
**classesディレクトリを一括で削除するワンライナー [#x2da5...
rm -rf `find ./ -type d -name classes`
**ファイル <-> byte[]に変換するUtil [#xf90100f]
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
/*******************************************************...
* Copyright (c) 2007 Masatomi KINO. All rights reserved.
* $Id$
*******************************************************...
public class Utils {
public static byte[] file2Byte(String file) throws IOE...
InputStream in = null;
byte[] pix = null;
try {
in = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputSt...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1) {
baos.write(b, 0, j);
}
pix = baos.toByteArray();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pix;
}
public static void byte2File(byte[] b, String outputFi...
throws IOException {
BufferedOutputStream stream = null;
try {
File file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(fi...
stream = new BufferedOutputStream(fstream);
stream.write(b);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
** 整数を16進や2進で表示する [#nd33ef2c]
System.out.println(Integer.toHexString(15));
System.out.println(Integer.toBinaryString(15));
実行結果は以下の通り。
f
1111
-[[16進数とは>http://www.kab-studio.biz/Programing/JavaA2...
**Base64でエンコードする [#i2fb78c1]
Base64とはE-mailなどバイナリデータを使用できない環境で、...
[[Commons Codec>http://jakarta.apache.org/commons/codec/]...
public class Base64Main {
public static String encode(String input) {
byte[] bs = Base64.encodeBase64(input.getBytes(), fa...
// booleanはある単位で改行を入れるかどうか
return new String(bs);
}
public static void main(String[] args) {
String string = encode("kino");
System.out.println(string);
}
}
実行結果は以下の通り
a2lubw==
簡単ですね。引数にbyte[]を取るので、バイナリファイルからb...
ちなみにこのCodecライブラリは下に出てくる byte[] -> HexSt...
new String(Hex.encodeHex(b));
とすればOKです。
あ、よく見たらSHA-1でハッシュするメソッドもありました。
System.out.println(DigestUtils.shaHex("kino".getBytes()));
でしたと同じ結果を得ることができます。実行結果は以下の通...
c108a2616c020deeb71df83906f039e3dfcc6752
おなじですね。
**SHA1でハッシュ化 [#q33bfba0]
SHA-1はパスワードのハッシュ化などに使われるアルゴリズムで...
public static String encrypt(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(input.getBytes("MS932"));
return toHexString(md.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private static String toHexString(byte[] b) {
StringBuffer hexString = new StringBuffer();
String plainText=null;
for (int i = 0; i < b.length; i++) {
plainText = Integer.toHexString(0xFF & b[i]);
if (plainText.length() < 2) {
plainText = "0" + plainText;
}
hexString.append(plainText);
}
return new String(hexString);
}
public static void main(String[] args) {
String string = encrypt("kino");
System.out.println(string);
}
実行結果は以下の通り。
c108a2616c020deeb71df83906f039e3dfcc6752
SHA1でハッシュ化できました。MessageDigest.getInstance("SH...
**java.lang.ThreadLocalについて [#wf809851]
thlocal.set( new LegacySystemImpl2());
thlocal.set( new LegacySystemImpl());
LegacySystem impl = (LegacySystem) thlocal.get();
などと同じ型のクラスをセットした場合、どうも上書きになる...
http://www.hyuki.com/dp/dpinfo_ThreadSpecificStorage.html...
**Eclipseのヒープとタスクマネージャの関係 [#h1eae651]
Eclipseのヒープサイズと、タスクマネージャのjavawのメモリ...
1024M中の500M
とかなってる場合、1024Mの方がタスクマネージャ上に表示され...
** EclipseでCVSのブランチを取り扱う。 [#e7289d1e]
CVSのトランク(メインっていう?)とブランチをマスターしたく...
まずはバージョンとしてタグ付け。タグ名はたとえば
V20061026_01
とする。次にブランチの作成。チーム >> ブランチ より作成。...
V20061026_01_BRANCH
とする。そのとき、ブランチをマージするための開始点のタグ...
Root_V20061026_01_BRANCH
というタグ名になる。メインのモジュールにブランチをマージ...
ここまででCVSには上の3つのタグ
V20061026_01 メイン側
V20061026_01_BRANCH ブランチ側
Root_V20061026_01_BRANCH メイン側
ができる。
Eclipseプロジェクトに、メインとブランチ両方落としておく。...
Eclipseで各プロジェクトでソースを修正してコミットする。
メイン側はバージョンは 1.1 -> 1.2
ブランチ側はバージョンは 1.1 -> 1.1.2.1
となるみたい。
さあ、ブランチからメインへマージします。メインのプロジェ...
マージされるバージョン(終了タグ) をブランチのバージョン ...
共通基本バージョン(開始タグ) をさっきの開始バージョ...
と選択すると、左がメイン、右がブランチのソース比較画面が...
※これはローカルコピーに、開始タグと終了タグの差分を取り出...
**実行中のメソッド名を取得する。 [#c5807e06]
new Throwable().getStackTrace()[0].getMethodName();
何故これでメソッド名が取れるんだろう。。。でもうまくいき...
**byte配列をバイナリに変換する。 [#b141cf0a]
逆に、byte[]をファイルに書き出す方法
public static void write(byte[] b) {
BufferedOutputStream stream = null;
try {
File file = new File("after.bin");
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
**バイナリファイルをbyte配列に変換する [#lbd2e6f7]
Webサービスでバイナリをアップするときなどは、byte[]で渡す...
public byte[] createBin() {
InputStream in = null;
byte[] pix = null;
try {
in = new FileInputStream("before.bin"); <-バイナリフ...
ByteArrayOutputStream baos = new ByteArrayOutputStre...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1) {
baos.write(b, 0, j);
}
pix = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pix;
}
**Apache Axisで、プロキシーを越える。 [#l2d74872]
Apache Axisを使ったWEBサービスのProxyクライアントで、HTTP...
いやあEclipseでソースにブレークポイントつけまくって、探し...
org.apache.axis.components.net.DefaultHTTPTransportClie...
てクラスが、なにやらプロパティをセットしていて、そこがAxi...
AxisProperties.setProperty("http.proxyHost", "proxyのサ...
AxisProperties.setProperty("http.proxyPort", "proxyのポ...
AxisProperties.setProperty("http.proxyUser", "xxxx");
AxisProperties.setProperty("http.proxyPassword", "xxxx");
越えた。。キター。。。
**Apache Axisで、自己署名証明書を使用してSSLさせる方法 [#...
-[[中里一日記: Axisにオレオレ証明書を食わせる方法>http://...
すげー
** HSQLDBの起動、フロントエンドの起動 [#lb92f311]
-データベースサーバの起動
java -cp /opt/hsqldb/hsqldb.jar org.hsqldb.Server
-database /tmp/bd -port 9001 -system_exit=true
-フロントエンドの起動
java -cp lib/hsqldb.jar org.hsqldb.util.DatabaseManager
** ブラウザのCookieを手軽に参照する [#hc2bdd7f]
JavaのTIPではないけど、J2EE開発中によく使うので。ブラウザ...
javascript:document.cookie;
と入力すると、クライアントのCookie(今開いているサイトに対...
** Jakarta Commons Langのいろいろ [#pdcb3fd8]
[[Jakarta Commons Lang:http://jakarta.jp/commons/lang.htm...
-toString()を実装するときに使う
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.append("title",getTitle())
.toString();
}
とかで
nu.mine.kino.rss.hibernate.Rss@33441dfb[id=10,title=hoge]
と出力できたり、
-equals()を実装するときに使う
public boolean equals(Object other) {
if (!(other instanceof Rss)){
return false;
}
Rss castOther = (Rss) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
とかで、getIdが同じならtrue、なんてことができたりします。...
**フィールドをカプセル化する [#h2d5feab]
設定ファイルとかの場所がフィールドに定数で埋め込まれてい...
例としてテスト対象クラスがHogeだとして
public class Hoge{
private String path ="hoge/hogehoge.properties";
.....
public void exe(){
new File(path).....;
}
}
なんてのがあるとき、どうすればよいかってことです。
この場合、まずはソースを変更して単体テストしやすくします...
- フィールドへのアクセスをgetter経由にする(getterはデフォ...
- 単体テスト時、anonymousクラスにして、そのgetterをoverri...
具体的には上のクラスは以下のように変更します。
- フィールドへのアクセスをgetter経由にする(getterはデフォ...
public class Hoge{
private String path ="hoge/hogehoge.properties";
.....
public void exe(){
new File(getPath()).....;
}
String getPath(){
return path;
}
}
- 単体テスト時、anonymousクラスにして、そのgetterをoverri...
public class HogeTest{
private Hoge hoge;
.....
public void testExe(){
hoge=new Hoge(){
String getPath(){
return "新しいパス";
}
};
hoge.exe();.......
}
}
こうするとパスを変更することができます。気になるのは
-フィールドを公開しちゃってる(デフォルトはパッケージ内、p...
-ソースを変更している
-anonymousのクラスに対するテストが、元のクラスのテストと...
ってところですね。。ソースの変更に関してはEclipseなどで論...
変数を参照している箇所(new File(path)の"path")を選択して...
定数がフィールドにすらいない場合は、上の前にさらにこれを...
変数を選択して、 リファクタリング >> ローカル変数を フィ...
**プレースホルダを使う [#w78f5e80]
MessageFormatを使うと、{0}のようなプレースホルダを使うこ...
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
SimpleDateFormat MM = new SimpleDateFormat("MM");
SimpleDateFormat dd = new SimpleDateFormat("dd");
Date selectedDate = new Date();
String year = yyyy.format(selectedDate);
String month = MM.format(selectedDate);
String date = dd.format(selectedDate);
Object[] dateArgs = { year, month, date };
MessageFormat form = new MessageFormat("{0}年{1}月{2}日"...
System.out.println(form.format(dateArgs));
実行結果は
2005年03月18日
**e.printStackTrace()の文字列を取得する、出力先を変更する...
例外のインスタンスをexとして
StringWriter stringWriter = new StringWriter();
ex.printStackTrace(new PrintWriter(stringWriter));
String message = stringWriter.getBuffer().toString();
**WSAD5.1.2でサーバの設定が格納されているディレクトリ [#t...
${WORKSPACE}\.metadata\.plugins\com.ibm.wtp.server.core\...
**プロクシ認証などなど [#u29f0f6c]
-Dhttp.proxyHost=[プロクシサーバ名] -Dhttp.proxyPort=[ポ...
-Dhttp.proxyUser=[UserID] -Dhttp.proxyPassword=[password]
** privateなフィールドにアクセスする [#x6b27408]
たとえばインスタンス名instance のフィールド(Stringで変数...
Class clazz = instance.getClass();
Field field = clazz.getDeclaredField("aField");
field.setAccessible(true);
String answer = (String)field.get(instance));
で取得できる。[[ここ:http://www.javadeveloper.jp/members/...
**<%@ page %> タグ内の意味。 [#j854efb6]
たとえば
<%@ page contentType=text/html; charset=EUC-JP" pageEnco...
とした場合、pageEncodingがソースコードの文字コード、conte...
** key=value&...の形式ではないPOSTリクエストの電文を取得...
requestからReaderを取得して、そこから文字を取得すればよい...
BufferedReader reader = request.getReader();
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(URLDecoder.decode(line));
buffer.append("\n");
}
String reqMsg = new String(buffer);
**DOM2String [#f98f8729]
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
TransformerFactory tfactory = TransformerFactory.newIn...
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty("encoding", encoding);
transformer.transform(new DOMSource(document), new Str...
} catch (TransformerConfigurationException e) {
logger_.error(e);
} catch (TransformerException e) {
logger_.error(e);
}
return out.toString();
**String2DOM [#h9052b8c]
例外処理はとりあえずおいといて
DocumentBuilderFactory factory = DocumentBuilderFactory....
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader stringReader = new StringReader(str); <- st...
InputSource inputSource = new InputSource(stringReader);
return builder.parse(inputSource); <- Document
** ファイル書きだし(バイナリで)[#hdf7a70e]
private void streamToFile(InputStream in, File file) thr...
ByteArrayOutputStream baos = new ByteArrayOutputStre...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1)
baos.write(b, 0, j);
byte[] pix = baos.toByteArray();
write(pix, file);
}
private void write(byte[] b, File file) {
BufferedOutputStream stream = null;
try {
FileOutputStream fstream = new FileOutputStream(...
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
** ファイル書きだし(テキスト) [#y7921044]
public boolean write(String log) {
BufferedWriter writer = null;
try {
//第二引数がtrueなら追記、falseなら上書き。
writer = new BufferedWriter(new FileWriter("./log.tx...
writer.write(log, 0, log.length());
writer.newLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (writer != null) {
writer.flush();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
** あるディレクトリからの相対パスを計算して、他のディレク...
うまく説明できませんが、別のディレクトリに同じパス構成を...
//たとえば "c:/temp", "c:/temp/fuga/aaaaa.txt", "d:/hug...
// d:/huge/hoho/fuga/aaaaa.txt としたい
public static String trimAndConcat(String base, String ...
URI uriBase = new File(base).toURI();
URI uriPath = new File(path).toURI();
URI uriRelativised = uriBase.relativize(uriPath);
return new File(other, uriRelativised.toString()).get...
}
**URLからファイルパスへ。 [#d93417e6]
URLクラスからそのファイルまでのパスを取得する方法です。
String path = new File(url.getPath()).getPath();
とやることで、
URL : file:/C:/hoge/config/hoge.txt
File: C:\hoge\config\hoge.txt
に変換ができます。((URLクラスをそのままtoStringすると、よ...
**(たとえば)拡張子がxmlのファイルをリストする [#y61287c6]
File mainDirectory = new File([ディレクトリ]);
String[] files = mainDirectory.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith("xml")) {
return true;
}
return false;
}
});
filesが拡張子がxmlのファイル(ファイル名、の配列)
** 拡張子より後ろの文字列を取得する [#x1ca454f]
String hoge="aaa.xml";
hoge.substring(hoge.lastIndexOf('.'));
で.xmlが取得できる。
** 拡張子より前の文字列を取得する [#n90b832d]
String hoge="aaa.xml";
hoge.substring(0, hoge.indexOf('.'));
でaaaが取得できる。/etc/aaa.xml.bakとかには使えないけど。。
** Javaから、WEBブラウザを起動(Windowsのみ) [#p27aaf30]
try {
Runtime.getRuntime().exec(
new String[] {
"rundll32.exe",
"url.dll,FileProtocolHandler",
"http://jp.sun.com/" });
} catch (IOException e) {
e.printStackTrace();
}
SWTから起動したい方は
[[こちら>SWT/TIPS集#ccfe5930]]
**WEBアプリケーションのルートを取得してパスを生成する [#c...
String root_path = this.getServletContext().getRealPath(...
String file_path = root_path + "hoge.dat"
** DOMオブジェクトをファイルに書き出す [#uf8d51a5]
try {
TransformerFactory tfactory = TransformerFactory.newIns...
Transformer transformer = tfactory.newTransformer();
// transformer.setOutputProperty("encoding", "EUC-JP");
transformer.transform(new DOMSource(document), <-docume...
//new StreamResult(System.out)); <- こっちだとコンソー...
new StreamResult(new FileOutputStream(new File(output))...
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
**JSPやServletのOutにDOMを書き出したい。 [#nd5a4674]
public void write(HttpServletRequest request, HttpServle...
response.setContentType("text/xml; charset=UTF-8");
try {
PrintWriter out = response.getWriter();
transformer.transform(new DOMSource(getDocument()),
new StreamResult(out));
} catch (IOException e) {
} catch (TransformerException e) {
}
}
** Servletで、コンテンツタイプを指定する。 [#q2320937]
response.setContentType("text/xml; charset=UTF-8");
**プロキシ越えとか、HttpConnection [#zaa38fb4]
#ref(http://www.masatom.in/cgi-bin/viewvc.cgi/*checkout*/...
//プロキシ認証の記述方法
BASE64Encoder encoder = new BASE64Encoder();
urlConnection.setRequestProperty(
"Proxy-Authorization",
"Basic " + encoder.encode((userid + ":" + password).get...
**javaの実行 [#p089762b]
>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt....
>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt....
kino.swt.JavaDocSearchForm
>java -classpath "クラスパス(セミコロン区切り)" クラス
[[このサイト:http://www.dmz.hitachi-sk.co.jp/Java/Tech/jr...
**Javadoc書き方 [#se0aae76]
@see UserInformationManager#getUserInformation(String)
とすると、リンクが張られる。
{@link InformationController InformationController}のフ...
とか
{@link クラス名#メンバ名 表示テキスト}
なんて使い方もできる
** コレクションを配列に変換 [#od8490a0]
type: 型
collection: コレクションクラス
(type[]) collection.toArray(new type[collection.size()]);
** 配列をコレクションに変換 [#vba52599]
fileList: Object[]型
java.util.Arrays.asList(fileList);
** ファイルを読み込む [#qbe6ff1a]
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(path));
//もしくは
//new BufferedReader(
// new InputStreamReader(url.openStream(), "JISAutoDet...
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.print
終了行:
#topicpath
----
あ、これってべんりだなあって思ったコーディングのメモ。
#contents
** AWS のLinuxでJavaを使う [#h783f118]
ふつうにwgetでrpm取ってきて、あとはJavaの切り替え。
wget --no-check-certificate --no-cookies --header \
"Cookie: oraclelicense=accept-securebackup-cookie" \
http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f...
sudo rpm -Uvh jdk-8u161-linux-x64.rpm
sudo alternatives --config java
export JAVA_HOME=/usr/java/jdk1.8.0_161
ちなみにダウンロード先はこちら
http://www.oracle.com/technetwork/java/javase/downloads/i...
** HttpURLConnection での接続において、Proxyを越える(SSL...
Jersey などライブラリを使ってる場合はこちら。。
[[JavaでSSLでRESTを投げるときに、プロキシを通す方法 - Qii...
ピュア(?)のHttpURLConnection を使ってるときはこんな感じで...
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketA...
final String authUser = "user";
final String authPassword = "pass";
Authenticator.setDefault(
new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentic...
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
HttpsURLConnection.setDefaultSSLSocketFactory( createSSL...
// setup a hostname verifier that verifies everything
HttpsURLConnection.setDefaultHostnameVerifier(createHost...
HttpsURLConnection con = (HttpsURLConnection)url.openCon...
// HttpURLConnection con = (HttpURLConnection)url...
createSSLContext()、createHostNameVerifier() メソッドは、...
** JavaでAWSのプロファイルを切り替える件 [#kcffb36a]
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.stan...
.withRegion(Regions.AP_NORTHEAST_1)
.withCredentials(new ProfileCredentialsProvider(...
.build();
こんな感じ。
参考:
http://www.masatom.in/pukiwiki/AWS/AWS%20CLI/#w1297a6a
** Mavenから AWS SDK for Java を使う場合 [#p24dc007]
-[[Apache Maven での SDK の使用 - AWS SDK for Java>https:...
**Jerseyのエラー [#dd13ed6b]
org.glassfish.jersey.message.internal.MessageBodyProvide...
MessageBodyWriter not found for media type=application/...
type=class java.util.HashMap, genericType=class java.uti...
ってエラーが出る場合。pom.xmlやクラスパスに、
<!-- jersyでJavaBeansをJSON でPOSTする場合。 -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
これが足りないから、かも!
**よく出てくるコマンド。 [#f2fc0494]
Eclipseの設定。
mvn -DdownloadSources=true -DdownloadJavadocs=true eclip...
Eclipse上からデバッグできるようにする
export MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket...
SpringBootの起動
mvn spring-boot:run
Jettyの起動
mvn jetty:run
ファイルをつくる。
$ cat << __EOF__ >> output.txt
> aaa
> bbb
> ccc
> __EOF__
$ cat output.txt
aaa
bbb
ccc
もはやJava関係ないな。。
**AWSのLinuxで Mavenを使う [#sb1c834a]
AWSのLinuxで、速攻でMavenでビルドしたくなって、しらべた内...
-[[Mavenをyumからインストール - Qiita>http://qiita.com/di...
-[[Amazon LinuxでJava8/Tomcat8の環境を構築する | Develop...
**Logの制御 [#pff20c16]
かつては Log4j がただ唯一の選択肢でしたが、どうやら乱立し...
-[[SLF4J+Lobackの基本 | Java好き>http://javazuki.com/arti...
SLF4J経由でLogback。ついでにLombokでAnnotation。これで決...
** 設定ファイルの制御 [#k95a870d]
ResourceBundleを使った設定ファイルの制御について。Resourc...
下記のサンプルは設定情報をクラスパス上のプロパティファイ...
package nu.mine.kino.resourcesamples;
import java.util.Enumeration;
import java.util.ResourceBundle;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Main {
public static void main(String[] args) {
new Main().execute();
}
// sample.properties をクラスパスから探して、なかっ...
// デフォルト値は、nu.mine.kino.resourcesamples.Defa...
private void execute() {
String propertyFile = "sample";
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle(propertyFi...
doSettings(bundle);
} catch (java.util.MissingResourceException e) {
String message = "設定ファイルが存在しません...
log.warn(message, propertyFile, e.getMessage...
doSettings(ResourceBundle.getBundle(
"nu.mine.kino.resourcesamples.Defaul...
}
}
private void doSettings(ResourceBundle bundle) {
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
log.debug("key[{}]:{}", key, bundle.getStrin...
}
}
}
package nu.mine.kino.resourcesamples;
import java.util.ListResourceBundle;
public class DefaultResources extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] { { "param1", "default val...
{ "param2", "default value2" } };
}
}
sample.properties
param1=property value1
param2=property value2
** ファイルパスの操作(Java7版) [#md2ed45b]
ファイルのパスをつなげるようなときに、もちろんStringでつ...
File base = new File("/tmp/base");
new File(new File(base, "more1"), "more2");
こんな風にくるんでくるんで、みたいな処理をしていたのが、
Paths.get("/tmp/base","more1","more2").toFile();
このように可変長引数を使ってつなげるようになりました。い...
** バイナリファイルをbyte配列に変換する(Java7版) [#va30b8...
便利な世の中になりました。
try {
byte[] bytes = Files.readAllBytes(Paths.get("パス"));
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
自前で書く必要なくなっちゃいました。AutoCloseableで自動で...
**ある配列に配列を追加して新しい配列を作る [#vfc530c0]
Object[] newArray = ArrayUtils.addAll(array1, array2);
**ある配列に単一のObjectを追加して新しい配列を作る [#j9ab...
Object[] array = ...;
Object element = ...;
Object[] newArray = ArrayUtils.add(array, element);
**Commonsのデリミタで分割するヤツ [#y2ba4cd9]
String[] splits = StringUtils.splitPreserveAllTokens(tar...
**StringBufferでカンマつなぎしたとき、最後のカンマはいら...
StringBuffer buf = new StringBuffer();
for (String str : array) {
buf.append(str);
buf.append(',');
}
buf.deleteCharAt(buf.length() - 1);
return new String(buf);
**わすれがちな、Dateの比較 before/after [#l032efcf]
Date base = new Date();
Date target = base;
System.out.println(base.after(target)); //おなじ日はfalse
System.out.println(base.before(target)); //おなじ日はfalse
System.out.println(base.before(DateUtils.addDays(target,...
System.out.println(base.after(DateUtils.addDays(target, ...
結果は
false
false
true
true
**バイト列を表示する。 [#j0b5477a]
テキストファイルが文字化けしてて、どんなバイナリの値なん...
private void printByte1(byte[] bytes) {
String hexString = toHexString(bytes);
System.out.println(hexString);
}
// byte[] -> 16進文字列へ変換
private String toHexString(byte[] b) {
StringBuffer hexString = new StringBuffer();
String plainText = null;
for (int i = 0; i < b.length; i++) {
plainText = Integer.toHexString(0xFF & b[i]);
if (plainText.length() < 2) {
plainText = "0" + plainText;
}
hexString.append(plainText);
hexString.append(" ");
}
return new String(hexString);
}
String#formatメソッドを使えば書式の指定で対応出来ますね。。
private void printByte2(byte[] bytes) {
for (byte b : bytes) {
System.out.printf(String.format("%1$x ", b));
}
System.out.println();
}
そもそもどうやってバイナリでファイル読むのっていうのは下...
**ファイルをバイナリで読み込む。。 [#t3df6b6b]
定番はコレですかね
String baseDirStr = "/tmp";
String fileName = "data.txt";
FileInputStream in = new FileInputStream(new File(baseDi...
int ch;
while ((ch = in.read()) != -1) {
// System.out.print(Integer.toHexString(ch) + " ");
System.out.printf(String.format("%1$x ", ch));
}
こういうのもある。。
RandomAccessFile accessFile = null;
try {
File file = new File(baseDirStr, fileName);
accessFile = new RandomAccessFile(file, "r");
int length = (int) file.length();
byte[] bytes = new byte[length];
accessFile.read(bytes);
printByte1(bytes);
printByte2(bytes);
} finally {
if (accessFile != null) {
accessFile.close();
}
}
RandomAccessFile にはバイナリファイルを readDouble とかや...
テキストファイルをバイナリで読んでみるときとかは前者が良...
**数値の文字列フォーマット変換 [#hb5bb0e4]
doubleとかの数値をフォーマッティングした文字列を取得する...
String.format("%1$,.0f" + " KB中、" + "%2$,.0f"+ " KB処...
a / 1024.0, b / 1024.0, c / 1024.0);
なかのプレースホルダは、それぞれ
%1$ -> a / 1024.0
%2$ -> b / 1024.0
%3$ -> c / 1024.0
って可変長の引数に対応。さらに
,.0f
で、「3ケタでカンマ区切り」「小数点以下は0ケタ」って意...
-[[数値の文字列フォーマット変換 ( Javaサンプル集 )>http:/...
-[[Java書式付き出力メモ(Hishidama's Java Formatter/Format...
**java.util.Date -> int [#t19069d3]
ついでに。。
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); <-2009/6/8に実行した
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year);
System.out.println(month);
System.out.println(day);
などで、Dateをintに変換可能です。
2009
5
8
Monthだけは日本人の感覚と一つ値がずれるので注意。
同様に、Calendar#set(int year, int month, int date) 、、...
Calendar calendar = Calendar.getInstance();
calendar.clear(); <-下で時分を指定しないから、clearしな...
calendar.set(year, month, day);
Date time = calendar.getTime();
とすることで、int -> java.util.DateもOKそうですね。
[[Calendar (Java 2 Platform SE 5.0)>http://java.sun.com/j...
**String -> java.util.Date [#w5cb107d]
文字列からjava.util.Dateを生成する方法ですが、
Date parseDate = new SimpleDateFormat("yyyy/MM/dd").pars...
System.out.println(parseDate);
などとやればできますが、Commons Langの org.apache.commons...
Date date = DateUtils.parseDate(dateStr, new String[] { ...
引数のパターンで順々にdataStrをパースして、Dateクラスを生...
**java.util.Date -> String [#i44fc664]
逆に、Dateから文字列を取得する方法。
String nowDateString = DateFormatUtils.format(nowDate, "...
System.out.println(nowDateString);
フォーマットの指定の仕方によって、年を取得、月を取得、日...
**「日」より下を切り捨てたDateを取得する [#z1f98e71]
日付だけ管理したいんだよなぁってときがよくありますが、Dat...
Date nowDate = new Date();
System.out.println("Today: " + nowDate);
// 「日」より下を切り捨て
Date dateTruncate = DateUtils.truncate(nowDate, Calendar...
System.out.println("Today: " + dateTruncate + " <-「日」...
// 「月」より下を切り捨て
dateTruncate = DateUtils.truncate(nowDate, Calendar.MONT...
System.out.println("Today: " + dateTruncate + " <-「月」...
// 「年」より下を切り捨て
dateTruncate = DateUtils.truncate(nowDate, Calendar.YEAR);
System.out.println("Today: " + dateTruncate + " <-「年」...
結果は以下の通り:
Today: Mon Jun 08 10:00:54 JST 2009
Today: Mon Jun 08 00:00:00 JST 2009 <-「日」より下を切り...
Today: Mon Jun 01 00:00:00 JST 2009 <-「月」より下を切り...
Today: Thu Jan 01 00:00:00 JST 2009 <-「年」より下を切り...
**stackTraceを取得する [#mcb50e85]
標準エラーに出力するならe.printStackTrace()ですが、文字列...
private String exception2String(Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream...
e.printStackTrace(new PrintStream(baos));
return baos.toString();
}
文字コードとか大丈夫だっけとかありますが、こんな感じでで...
**プロパティファイルから、複数のデータを配列で取得する [#...
[[Java Collections Framework>http://commons.apache.org/co...
-hoge.properties
data=hoge@fuga.com
data=hogehoge@fuga.jp
などという複数のデータを配列で取得することができます。こ...
上記データにアクセスするサンプルコードは以下の通り。
ExtendedProperties props = new ExtendedProperties();
ClassLoader loader = Thread.currentThread().getContextCl...
InputStream resourceAsStream = loader
.getResourceAsStream("hoge.properties");
InputStream in = new BufferedInputStream(resourceAsStrea...
props.load(in);
String[] dataArray = props.getStringArray("data");
などとしてデータを取り出すことができます。
-[[リソースへのアクセス>http://java.sun.com/j2se/1.5.0/ja...
-[[プロパティファイルから、String配列の値を取り込むには?...
-[[ExtendedPropertiesの自動読み込みについて - Java Soluti...
-[[Commons Collections>http://muimi.com/j/jakarta/commons...
**パイプされた標準入力を取得する [#uf4f1804]
そういえばJavaって標準入力をどう受け取るんだっけ??とい...
package nu.mine.kino.mail.utils;
import java.io.BufferedInputStream;
import java.io.IOException;
public class Echo {
public static void main(String[] args) {
BufferedInputStream in = new BufferedInputStream...
try {
int size = in.available();
byte[] bytes = new byte[size];
in.read(bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
}
結果はたとえば
[hogehoge@www Test]$ ls | java -cp lib/sample.jar nu.m...
build.win.properties
build_jar.xml
classes
lib
source
[hogehoge@www Test]$
lsの結果を受け取り、echoできました。
-シェルからJavaを呼び出すときもOK
[hogehoge@www Test]$ cat sample.sh
export JAVA_HOME=/opt/jdk1.5.0_12
export LIBDIR=lib
export CLASSPATH=${LIBDIR}/sample.jar
java nu.mine.kino.mail.utils.Echo
[hogehoge@www Test]$ ls | ./sample.sh
build.win.properties
build_jar.xml
classes
lib
sample.sh
source
[hogehoge@www Test]$
ちなみに標準入力を待ち受けてるときに入力完了を通知するた...
[[Platform-Specific Details: The End-of-Input Character>h...
参考
-[[標準入出力ストリーム>http://www.ei.fukui-nct.ac.jp/~yf...
**Apache Axis 1.4のSSL処理にはバグがありそう [#f0df163f]
詳しくはまた時間がとれたらにしますが、Basic認証するプロキ...
-[[Svn内のソース>http://svn.apache.org/viewvc/webservices...
**BigDecimalのコンストラクタにdoubleは使わないほうがいい ...
ちゃんと数値計算しようと思って、doubleとかの代わりにBigDe...
って、よく見たらJavaDocにも書いてありますね。
-[[BigDecimal>http://docs.oracle.com/javase/jp/6/api/java...
**classesディレクトリを一括で削除するワンライナー [#x2da5...
rm -rf `find ./ -type d -name classes`
**ファイル <-> byte[]に変換するUtil [#xf90100f]
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
/*******************************************************...
* Copyright (c) 2007 Masatomi KINO. All rights reserved.
* $Id$
*******************************************************...
public class Utils {
public static byte[] file2Byte(String file) throws IOE...
InputStream in = null;
byte[] pix = null;
try {
in = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputSt...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1) {
baos.write(b, 0, j);
}
pix = baos.toByteArray();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pix;
}
public static void byte2File(byte[] b, String outputFi...
throws IOException {
BufferedOutputStream stream = null;
try {
File file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(fi...
stream = new BufferedOutputStream(fstream);
stream.write(b);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
** 整数を16進や2進で表示する [#nd33ef2c]
System.out.println(Integer.toHexString(15));
System.out.println(Integer.toBinaryString(15));
実行結果は以下の通り。
f
1111
-[[16進数とは>http://www.kab-studio.biz/Programing/JavaA2...
**Base64でエンコードする [#i2fb78c1]
Base64とはE-mailなどバイナリデータを使用できない環境で、...
[[Commons Codec>http://jakarta.apache.org/commons/codec/]...
public class Base64Main {
public static String encode(String input) {
byte[] bs = Base64.encodeBase64(input.getBytes(), fa...
// booleanはある単位で改行を入れるかどうか
return new String(bs);
}
public static void main(String[] args) {
String string = encode("kino");
System.out.println(string);
}
}
実行結果は以下の通り
a2lubw==
簡単ですね。引数にbyte[]を取るので、バイナリファイルからb...
ちなみにこのCodecライブラリは下に出てくる byte[] -> HexSt...
new String(Hex.encodeHex(b));
とすればOKです。
あ、よく見たらSHA-1でハッシュするメソッドもありました。
System.out.println(DigestUtils.shaHex("kino".getBytes()));
でしたと同じ結果を得ることができます。実行結果は以下の通...
c108a2616c020deeb71df83906f039e3dfcc6752
おなじですね。
**SHA1でハッシュ化 [#q33bfba0]
SHA-1はパスワードのハッシュ化などに使われるアルゴリズムで...
public static String encrypt(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(input.getBytes("MS932"));
return toHexString(md.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
private static String toHexString(byte[] b) {
StringBuffer hexString = new StringBuffer();
String plainText=null;
for (int i = 0; i < b.length; i++) {
plainText = Integer.toHexString(0xFF & b[i]);
if (plainText.length() < 2) {
plainText = "0" + plainText;
}
hexString.append(plainText);
}
return new String(hexString);
}
public static void main(String[] args) {
String string = encrypt("kino");
System.out.println(string);
}
実行結果は以下の通り。
c108a2616c020deeb71df83906f039e3dfcc6752
SHA1でハッシュ化できました。MessageDigest.getInstance("SH...
**java.lang.ThreadLocalについて [#wf809851]
thlocal.set( new LegacySystemImpl2());
thlocal.set( new LegacySystemImpl());
LegacySystem impl = (LegacySystem) thlocal.get();
などと同じ型のクラスをセットした場合、どうも上書きになる...
http://www.hyuki.com/dp/dpinfo_ThreadSpecificStorage.html...
**Eclipseのヒープとタスクマネージャの関係 [#h1eae651]
Eclipseのヒープサイズと、タスクマネージャのjavawのメモリ...
1024M中の500M
とかなってる場合、1024Mの方がタスクマネージャ上に表示され...
** EclipseでCVSのブランチを取り扱う。 [#e7289d1e]
CVSのトランク(メインっていう?)とブランチをマスターしたく...
まずはバージョンとしてタグ付け。タグ名はたとえば
V20061026_01
とする。次にブランチの作成。チーム >> ブランチ より作成。...
V20061026_01_BRANCH
とする。そのとき、ブランチをマージするための開始点のタグ...
Root_V20061026_01_BRANCH
というタグ名になる。メインのモジュールにブランチをマージ...
ここまででCVSには上の3つのタグ
V20061026_01 メイン側
V20061026_01_BRANCH ブランチ側
Root_V20061026_01_BRANCH メイン側
ができる。
Eclipseプロジェクトに、メインとブランチ両方落としておく。...
Eclipseで各プロジェクトでソースを修正してコミットする。
メイン側はバージョンは 1.1 -> 1.2
ブランチ側はバージョンは 1.1 -> 1.1.2.1
となるみたい。
さあ、ブランチからメインへマージします。メインのプロジェ...
マージされるバージョン(終了タグ) をブランチのバージョン ...
共通基本バージョン(開始タグ) をさっきの開始バージョ...
と選択すると、左がメイン、右がブランチのソース比較画面が...
※これはローカルコピーに、開始タグと終了タグの差分を取り出...
**実行中のメソッド名を取得する。 [#c5807e06]
new Throwable().getStackTrace()[0].getMethodName();
何故これでメソッド名が取れるんだろう。。。でもうまくいき...
**byte配列をバイナリに変換する。 [#b141cf0a]
逆に、byte[]をファイルに書き出す方法
public static void write(byte[] b) {
BufferedOutputStream stream = null;
try {
File file = new File("after.bin");
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
**バイナリファイルをbyte配列に変換する [#lbd2e6f7]
Webサービスでバイナリをアップするときなどは、byte[]で渡す...
public byte[] createBin() {
InputStream in = null;
byte[] pix = null;
try {
in = new FileInputStream("before.bin"); <-バイナリフ...
ByteArrayOutputStream baos = new ByteArrayOutputStre...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1) {
baos.write(b, 0, j);
}
pix = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pix;
}
**Apache Axisで、プロキシーを越える。 [#l2d74872]
Apache Axisを使ったWEBサービスのProxyクライアントで、HTTP...
いやあEclipseでソースにブレークポイントつけまくって、探し...
org.apache.axis.components.net.DefaultHTTPTransportClie...
てクラスが、なにやらプロパティをセットしていて、そこがAxi...
AxisProperties.setProperty("http.proxyHost", "proxyのサ...
AxisProperties.setProperty("http.proxyPort", "proxyのポ...
AxisProperties.setProperty("http.proxyUser", "xxxx");
AxisProperties.setProperty("http.proxyPassword", "xxxx");
越えた。。キター。。。
**Apache Axisで、自己署名証明書を使用してSSLさせる方法 [#...
-[[中里一日記: Axisにオレオレ証明書を食わせる方法>http://...
すげー
** HSQLDBの起動、フロントエンドの起動 [#lb92f311]
-データベースサーバの起動
java -cp /opt/hsqldb/hsqldb.jar org.hsqldb.Server
-database /tmp/bd -port 9001 -system_exit=true
-フロントエンドの起動
java -cp lib/hsqldb.jar org.hsqldb.util.DatabaseManager
** ブラウザのCookieを手軽に参照する [#hc2bdd7f]
JavaのTIPではないけど、J2EE開発中によく使うので。ブラウザ...
javascript:document.cookie;
と入力すると、クライアントのCookie(今開いているサイトに対...
** Jakarta Commons Langのいろいろ [#pdcb3fd8]
[[Jakarta Commons Lang:http://jakarta.jp/commons/lang.htm...
-toString()を実装するときに使う
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.append("title",getTitle())
.toString();
}
とかで
nu.mine.kino.rss.hibernate.Rss@33441dfb[id=10,title=hoge]
と出力できたり、
-equals()を実装するときに使う
public boolean equals(Object other) {
if (!(other instanceof Rss)){
return false;
}
Rss castOther = (Rss) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
とかで、getIdが同じならtrue、なんてことができたりします。...
**フィールドをカプセル化する [#h2d5feab]
設定ファイルとかの場所がフィールドに定数で埋め込まれてい...
例としてテスト対象クラスがHogeだとして
public class Hoge{
private String path ="hoge/hogehoge.properties";
.....
public void exe(){
new File(path).....;
}
}
なんてのがあるとき、どうすればよいかってことです。
この場合、まずはソースを変更して単体テストしやすくします...
- フィールドへのアクセスをgetter経由にする(getterはデフォ...
- 単体テスト時、anonymousクラスにして、そのgetterをoverri...
具体的には上のクラスは以下のように変更します。
- フィールドへのアクセスをgetter経由にする(getterはデフォ...
public class Hoge{
private String path ="hoge/hogehoge.properties";
.....
public void exe(){
new File(getPath()).....;
}
String getPath(){
return path;
}
}
- 単体テスト時、anonymousクラスにして、そのgetterをoverri...
public class HogeTest{
private Hoge hoge;
.....
public void testExe(){
hoge=new Hoge(){
String getPath(){
return "新しいパス";
}
};
hoge.exe();.......
}
}
こうするとパスを変更することができます。気になるのは
-フィールドを公開しちゃってる(デフォルトはパッケージ内、p...
-ソースを変更している
-anonymousのクラスに対するテストが、元のクラスのテストと...
ってところですね。。ソースの変更に関してはEclipseなどで論...
変数を参照している箇所(new File(path)の"path")を選択して...
定数がフィールドにすらいない場合は、上の前にさらにこれを...
変数を選択して、 リファクタリング >> ローカル変数を フィ...
**プレースホルダを使う [#w78f5e80]
MessageFormatを使うと、{0}のようなプレースホルダを使うこ...
SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
SimpleDateFormat MM = new SimpleDateFormat("MM");
SimpleDateFormat dd = new SimpleDateFormat("dd");
Date selectedDate = new Date();
String year = yyyy.format(selectedDate);
String month = MM.format(selectedDate);
String date = dd.format(selectedDate);
Object[] dateArgs = { year, month, date };
MessageFormat form = new MessageFormat("{0}年{1}月{2}日"...
System.out.println(form.format(dateArgs));
実行結果は
2005年03月18日
**e.printStackTrace()の文字列を取得する、出力先を変更する...
例外のインスタンスをexとして
StringWriter stringWriter = new StringWriter();
ex.printStackTrace(new PrintWriter(stringWriter));
String message = stringWriter.getBuffer().toString();
**WSAD5.1.2でサーバの設定が格納されているディレクトリ [#t...
${WORKSPACE}\.metadata\.plugins\com.ibm.wtp.server.core\...
**プロクシ認証などなど [#u29f0f6c]
-Dhttp.proxyHost=[プロクシサーバ名] -Dhttp.proxyPort=[ポ...
-Dhttp.proxyUser=[UserID] -Dhttp.proxyPassword=[password]
** privateなフィールドにアクセスする [#x6b27408]
たとえばインスタンス名instance のフィールド(Stringで変数...
Class clazz = instance.getClass();
Field field = clazz.getDeclaredField("aField");
field.setAccessible(true);
String answer = (String)field.get(instance));
で取得できる。[[ここ:http://www.javadeveloper.jp/members/...
**<%@ page %> タグ内の意味。 [#j854efb6]
たとえば
<%@ page contentType=text/html; charset=EUC-JP" pageEnco...
とした場合、pageEncodingがソースコードの文字コード、conte...
** key=value&...の形式ではないPOSTリクエストの電文を取得...
requestからReaderを取得して、そこから文字を取得すればよい...
BufferedReader reader = request.getReader();
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(URLDecoder.decode(line));
buffer.append("\n");
}
String reqMsg = new String(buffer);
**DOM2String [#f98f8729]
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
TransformerFactory tfactory = TransformerFactory.newIn...
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty("encoding", encoding);
transformer.transform(new DOMSource(document), new Str...
} catch (TransformerConfigurationException e) {
logger_.error(e);
} catch (TransformerException e) {
logger_.error(e);
}
return out.toString();
**String2DOM [#h9052b8c]
例外処理はとりあえずおいといて
DocumentBuilderFactory factory = DocumentBuilderFactory....
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader stringReader = new StringReader(str); <- st...
InputSource inputSource = new InputSource(stringReader);
return builder.parse(inputSource); <- Document
** ファイル書きだし(バイナリで)[#hdf7a70e]
private void streamToFile(InputStream in, File file) thr...
ByteArrayOutputStream baos = new ByteArrayOutputStre...
byte[] b = new byte[1024];
int j;
while ((j = in.read(b)) != -1)
baos.write(b, 0, j);
byte[] pix = baos.toByteArray();
write(pix, file);
}
private void write(byte[] b, File file) {
BufferedOutputStream stream = null;
try {
FileOutputStream fstream = new FileOutputStream(...
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
** ファイル書きだし(テキスト) [#y7921044]
public boolean write(String log) {
BufferedWriter writer = null;
try {
//第二引数がtrueなら追記、falseなら上書き。
writer = new BufferedWriter(new FileWriter("./log.tx...
writer.write(log, 0, log.length());
writer.newLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (writer != null) {
writer.flush();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
** あるディレクトリからの相対パスを計算して、他のディレク...
うまく説明できませんが、別のディレクトリに同じパス構成を...
//たとえば "c:/temp", "c:/temp/fuga/aaaaa.txt", "d:/hug...
// d:/huge/hoho/fuga/aaaaa.txt としたい
public static String trimAndConcat(String base, String ...
URI uriBase = new File(base).toURI();
URI uriPath = new File(path).toURI();
URI uriRelativised = uriBase.relativize(uriPath);
return new File(other, uriRelativised.toString()).get...
}
**URLからファイルパスへ。 [#d93417e6]
URLクラスからそのファイルまでのパスを取得する方法です。
String path = new File(url.getPath()).getPath();
とやることで、
URL : file:/C:/hoge/config/hoge.txt
File: C:\hoge\config\hoge.txt
に変換ができます。((URLクラスをそのままtoStringすると、よ...
**(たとえば)拡張子がxmlのファイルをリストする [#y61287c6]
File mainDirectory = new File([ディレクトリ]);
String[] files = mainDirectory.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith("xml")) {
return true;
}
return false;
}
});
filesが拡張子がxmlのファイル(ファイル名、の配列)
** 拡張子より後ろの文字列を取得する [#x1ca454f]
String hoge="aaa.xml";
hoge.substring(hoge.lastIndexOf('.'));
で.xmlが取得できる。
** 拡張子より前の文字列を取得する [#n90b832d]
String hoge="aaa.xml";
hoge.substring(0, hoge.indexOf('.'));
でaaaが取得できる。/etc/aaa.xml.bakとかには使えないけど。。
** Javaから、WEBブラウザを起動(Windowsのみ) [#p27aaf30]
try {
Runtime.getRuntime().exec(
new String[] {
"rundll32.exe",
"url.dll,FileProtocolHandler",
"http://jp.sun.com/" });
} catch (IOException e) {
e.printStackTrace();
}
SWTから起動したい方は
[[こちら>SWT/TIPS集#ccfe5930]]
**WEBアプリケーションのルートを取得してパスを生成する [#c...
String root_path = this.getServletContext().getRealPath(...
String file_path = root_path + "hoge.dat"
** DOMオブジェクトをファイルに書き出す [#uf8d51a5]
try {
TransformerFactory tfactory = TransformerFactory.newIns...
Transformer transformer = tfactory.newTransformer();
// transformer.setOutputProperty("encoding", "EUC-JP");
transformer.transform(new DOMSource(document), <-docume...
//new StreamResult(System.out)); <- こっちだとコンソー...
new StreamResult(new FileOutputStream(new File(output))...
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
**JSPやServletのOutにDOMを書き出したい。 [#nd5a4674]
public void write(HttpServletRequest request, HttpServle...
response.setContentType("text/xml; charset=UTF-8");
try {
PrintWriter out = response.getWriter();
transformer.transform(new DOMSource(getDocument()),
new StreamResult(out));
} catch (IOException e) {
} catch (TransformerException e) {
}
}
** Servletで、コンテンツタイプを指定する。 [#q2320937]
response.setContentType("text/xml; charset=UTF-8");
**プロキシ越えとか、HttpConnection [#zaa38fb4]
#ref(http://www.masatom.in/cgi-bin/viewvc.cgi/*checkout*/...
//プロキシ認証の記述方法
BASE64Encoder encoder = new BASE64Encoder();
urlConnection.setRequestProperty(
"Proxy-Authorization",
"Basic " + encoder.encode((userid + ":" + password).get...
**javaの実行 [#p089762b]
>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt....
>java -classpath ".;lib;classes;lib\log4j-1.2.8.jar;swt....
kino.swt.JavaDocSearchForm
>java -classpath "クラスパス(セミコロン区切り)" クラス
[[このサイト:http://www.dmz.hitachi-sk.co.jp/Java/Tech/jr...
**Javadoc書き方 [#se0aae76]
@see UserInformationManager#getUserInformation(String)
とすると、リンクが張られる。
{@link InformationController InformationController}のフ...
とか
{@link クラス名#メンバ名 表示テキスト}
なんて使い方もできる
** コレクションを配列に変換 [#od8490a0]
type: 型
collection: コレクションクラス
(type[]) collection.toArray(new type[collection.size()]);
** 配列をコレクションに変換 [#vba52599]
fileList: Object[]型
java.util.Arrays.asList(fileList);
** ファイルを読み込む [#qbe6ff1a]
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(path));
//もしくは
//new BufferedReader(
// new InputStreamReader(url.openStream(), "JISAutoDet...
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.print
ページ名: