http://d.hatena.ne.jp/fumokmm/20110420/1303271968
http://itpro.nikkeibp.co.jp/article/COLUMN/20120417/391316/
String で switch 文
1 |
switch (str) { case "hoge": break; case "foo": break; case "bar": break; default: break; } |
マルチキャッチ
1 |
try { // 何か処理 } catch (NamingException | IOException e) { e.printStackTrace(); } |
型推論
Map<String, List<String>> map = new HashMap<>();
右辺は書かなくてもわかるでしょ? ってこと。
2進数リテラルと数値のアンダースコア区切り
1234_5678 1_2_3_4__5_6_7_8L // アンダースコアはいくつあってもOKのようです 0b0001_0010_0100_1000 3.141_592_653_589_793d 0x1.ffff_ffff_ffff_fP1_023 // Double.MAX_VALUE
数値をわかりやすいようにアンダースコアを使って好きな場所で区切っても良いってこと
try with resources
try句 で利用するリソースを自動的に close する機能。
いちいち finally句で close する手間が省ける。
ただし、処理順序に注意
- try
- close
- catch
- finally
の順で処理されるので、 catch後にfinallyでロールバックする等の処理をする場合は注意が必要。
動作順検証 例外が発生しない場合
1 |
public class tryWithResources { public static void main(String[] args){ try(closeTest t = new closeTest(false, false); closeTest t2 = new closeTest(false, false); closeTest t3 = new closeTest(false, false);) { System.out.println("try in"); } catch (Exception e){ System.out.println("catch"); System.out.println(" " + e); for (Throwable t : e.getSuppressed()) { System.out.println(" suppressed:" + t); } } finally { System.out.println("finally"); } } public static class closeTest implements AutoCloseable { private boolean throwInClose = false; public closeTest(boolean isThrow, boolean throwInClose) throws Exception { System.out.println("Constructor " + this.hashCode()); if (isThrow){ throw new Exception("Exception in Constructor " + this.hashCode()); } this.throwInClose = throwInClose; } public void close() throws Exception { System.out.println("close " + this.hashCode()); if (throwInClose){ throw new Exception("Exception in close " + this.hashCode()); } } } } |
出力結果
Constructor 11541827 Constructor 5324016 Constructor 24622029 try in close 24622029 close 5324016 close 11541827 finally
動作順検証 try句またはclose処理中に例外が発生する場合
1 |
public class tryWithResources { public static void main(String[] args){ try(closeTest t = new closeTest(false, false); closeTest t2 = new closeTest(false, true); closeTest t3 = new closeTest(false, false);) { System.out.println("try in"); } catch (Exception e){ System.out.println("catch"); System.out.println(" " + e); for (Throwable t : e.getSuppressed()) { System.out.println(" suppressed:" + t); } } finally { System.out.println("finally"); } } public static class closeTest implements AutoCloseable { private boolean throwInClose = false; public closeTest(boolean isThrow, boolean throwInClose) throws Exception { System.out.println("Constructor " + this.hashCode()); if (isThrow){ throw new Exception("Exception in Constructor " + this.hashCode()); } this.throwInClose = throwInClose; } public void close() throws Exception { System.out.println("close " + this.hashCode()); if (throwInClose){ throw new Exception("Exception in close " + this.hashCode()); } } } } |
出力結果
Constructor 29194312 Constructor 2352593 Constructor 12910198 try in close 12910198 close 2352593 close 29194312 catch java.lang.Exception: Exception in close 2352593 finally
closeの途中で例外が発生しても、全てのcloseが呼ばれる
動作順検証 リソースのコンストラクタで例外が発生した場合
1 |
public class tryWithResources { public static void main(String[] args){ try(closeTest t = new closeTest(false, false); closeTest t2 = new closeTest(true, false); closeTest t3 = new closeTest(false, false);) { System.out.println("try in"); } catch (Exception e){ System.out.println("catch"); System.out.println(" " + e); for (Throwable t : e.getSuppressed()) { System.out.println(" suppressed:" + t); } } finally { System.out.println("finally"); } } public static class closeTest implements AutoCloseable { private boolean throwInClose = false; public closeTest(boolean isThrow, boolean throwInClose) throws Exception { System.out.println("Constructor " + this.hashCode()); if (isThrow){ throw new Exception("Exception in Constructor " + this.hashCode()); } this.throwInClose = throwInClose; } public void close() throws Exception { System.out.println("close " + this.hashCode()); if (throwInClose){ throw new Exception("Exception in close " + this.hashCode()); } } } } |
出力結果
Constructor 24622029 Constructor 29194312 close 24622029 catch java.lang.Exception: Exception in Constructor 29194312 finally
既に生成されたリソースのcloseが呼ばれる
NIO2
基本的な使い方
Files クラスに Path オブジェクトを渡すことで操作する。
1 |
import java.nio.file.*; import java.nio.charset.*; import java.io.*; public class nioTest1 { public static void main(String[] args){ FileSystem fs = FileSystems.getDefault(); Path p1 = fs.getPath("nioTest1.java"); Path p2 = Paths.get("nioTest1.copy"); Path p3 = Paths.get("test","nioTest1.copy"); System.out.println(p1); System.out.println(p2); try { // コピー Files.copy(p1,p2,StandardCopyOption.REPLACE_EXISTING); // 移動 Files.move(p2,p3,StandardCopyOption.REPLACE_EXISTING); // 削除 Files.delete(p3); // ライター取得 BufferedWriter bw = Files.newBufferedWriter(p2,Charset.defaultCharset()); bw.write("test"); bw.close(); // リーダー取得 BufferedReader br = Files.newBufferedReader(p2,Charset.defaultCharset()); System.out.println(br.readLine()); br.close(); } catch (Exception e){ e.printStackTrace(); } } } |
FileSystem#getPath と Paths#get の違い
http://itpro.nikkeibp.co.jp/article/COLUMN/20110725/362803/?ST=develop
Pathsクラスのgetメソッドを使用すれば、FileSystemクラスのgetPathと同じようにPathオブジェクトを生成できます。ただし、Pathsクラスではデフォルトのファイルシステムしか扱うことができません。
ディレクトリ探索
FileVisitor を walkFileTree へ渡すことでディレクトリ探索ができる
1 |
import java.nio.file.*; import java.nio.charset.*; import java.nio.file.attribute.*; import java.io.*; public class nioTest2 { public static void main(String[] args){ FileSystem fs = FileSystems.getDefault(); Path p1 = Paths.get("test"); FileVisitor<Path> v = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { System.out.println(dir); return FileVisitResult.CONTINUE; } }; try { Files.walkFileTree(p1, v); } catch (Exception e){ e.printStackTrace(); } } } |
ディレクトリ監視
1 |
import java.nio.file.*; import java.nio.charset.*; import java.nio.file.attribute.*; import java.io.*; public class nioTest3 { public static void main(String[] args){ FileSystem fs = FileSystems.getDefault(); Path p1 = Paths.get("test"); try { WatchService s = fs.newWatchService(); p1.register(s, StandardWatchEventKinds.ENTRY_CREATE); while(true){ System.out.println("take"); WatchKey key = s.take(); for(WatchEvent<?> event: key.pollEvents()){ System.out.println(event); } } } catch (Exception e){ e.printStackTrace(); } } } |
[通知用URL]
Tweet
最終更新時間:2012年09月11日 22時54分23秒