Last active
August 29, 2015 14:19
-
-
Save toagit/7e00afcd968685ff2e20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//リソースを確保した順と逆順に確実にfinallyでリソースを解放する | |
try { | |
String path = ""; | |
BufferdReader br = null; | |
try { | |
File file = new File(path); | |
String encode = "UTF-8"; | |
// br = new BufferdReader(new FileReader(file)); | |
br = new BufferdReader(new InputStreamReader(new FileInputStream(file), encode)); | |
String outPath = file.getAbsolutePath() + ".out"; | |
BuffertWriter bw = null; | |
try { | |
//bw = new BufferdWriter(outpath); | |
bw = new BufferdWriter(new OutputStreamWriter(new FileOutputStream(outpath),encode)); | |
String line; | |
while ((line = br.readLine() != null) { | |
bw.write(line); | |
bw.newLine(); | |
} | |
bw.flush(); | |
} finally { | |
if (bw != null) { | |
bw.close(); | |
} | |
} | |
} finally { | |
if (br != null) { | |
br.close(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//変数へ全部のデータを一時的に保存してから、書き込みをする | |
try { | |
String path = ""; | |
BufferdReader br = null; | |
List lines = new LinkedList(); | |
String outPath; | |
try { | |
File file = new File(path); | |
br = new BufferdReader(new FileReader(file)); | |
outPath = file.getAbsolutePath() + ".out"; | |
String line; | |
while ((line = br.readLine() != null) { | |
lines.add(line); | |
} | |
} finally { | |
if (br != null) { | |
br.close(); | |
} | |
} | |
BuffertWriter bw = null; | |
try { | |
bw = new BufferdWriter(outpath) | |
for (String outLine : lines) { | |
bw.write(outLine); | |
bw.newLine(); | |
} | |
bw.flush(); | |
} finally { | |
if (bw != null) { | |
bw.close(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//リソースを確保した順と逆順に確実にfinallyでリソースを解放する | |
//PrintWriterはIOExceptionを発生しない | |
//PrintWriterは自動flushできるが、明示的にflushを呼び出したほうが効率がよい。 | |
try { | |
String path = ""; | |
BufferdReader br = null; | |
try { | |
File file = new File(path); | |
String encode = "UTF-8"; | |
br = new BufferdReader(new InputStreamReader(new FileInputStream(file), encode)); | |
String outPath = file.getAbsolutePath() + ".out"; | |
PrintWriter pw = null; | |
pw = newPrintWriter(new BufferdWriter(new OutputStreamWriter(new FileOutputStream(outpath), encode))); | |
String line; | |
while ((line = br.readLine() != null) { | |
pw.println(line); | |
} | |
pw.flush(); | |
pw.close(); | |
} finally { | |
if (br != null) { | |
br.close(); | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 例外処理省略版 | |
try { | |
String path = ""; | |
File file = new File(path); | |
BufferdReader br = new BufferdReader(new FileReader(file)); | |
// BufferdReader br = new BufferdReader(new InputStreamReader(new FileInputStream(file), encode)); | |
String outPath = file.getAbsolutePath() + ".out"; | |
BuffertWriter bw = new BufferdWriter(outpath) | |
//BuffertWriter bw = new BufferdWriter(new OutputStreamWriter(new FileOutputStream(outpath),encode)); | |
String line; | |
while ((line = br.readLine() != null) { | |
bw.write(line); | |
bw.newLine(); | |
} | |
bw.flush(); | |
bw.close(); | |
br.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment