Skip to content

Instantly share code, notes, and snippets.

@grim13b
Created September 5, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grim13b/32cb3da7be742084b0a8802265eebc65 to your computer and use it in GitHub Desktop.
Save grim13b/32cb3da7be742084b0a8802265eebc65 to your computer and use it in GitHub Desktop.
よくわからん例
package org.grim3lt.example.Hogehoge;
import lombok.Cleanup;
import java.io.*;
import java.util.Arrays;
public class Hogehoge {
public static void main(String[] args) {
Hogehoge hogehoge = new Hogehoge();
hogehoge.hoge1("/tekitouna/path/hoge.txt");
hogehoge.hoge2("/tekitouna/path/hoge.txt");
}
private void hoge1(String filename) {
try {
@Cleanup InputStream in = new FileInputStream(filename);
byte[] hoge = new byte[1024];
while(in.read(hoge) != -1) {
System.out.println("hoge1" + Arrays.toString(hoge));
// このあたりで例外投げても
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void hoge2(String filename) {
try (InputStream in = new FileInputStream(filename)) {
byte[] hoge = new byte[1024];
while(in.read(hoge) != -1) {
System.out.println("hoge2" + Arrays.toString(hoge));
// このあたりで例外投げても
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Nest的には変わらないし in のスコープが Cleanup だと
// 見づらいしというだけの理由
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment