Upgrade to Pro — share decks privately, control downloads, hide ads and more …

from old Java to modern Java (2017) #jjug

from old Java to modern Java (2017) #jjug

JJUG ナイトセミナー 「Java エンジニアのためのJava(再)入門」
https://jjug.doorkeeper.jp/events/61857

Shin Tanimoto

June 28, 2017
Tweet

More Decks by Shin Tanimoto

Other Decks in Programming

Transcript

  1. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 8IPBN* • ୩ຊ ৺ (Shin Tanimoto) • Acroquest Technologyגࣜձࣾ • ೔ຊJavaϢʔβάϧʔϓ (JJUG) • JavaOne / SpringOneεϐʔΧʔ • Microservices, Elasticsearch
 • twitter: @cero_t • facebook: shin.tanimoto • 9݄16೔ੜ·Ε 2
  2. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug ॻ੶Λॻ͖·ͨ͠ • Javaຊ֨ೖ໳ େ޷ධൃചதʂ • ॻ࣌͘ʹେ੾ʹͨ͜͠ͱ • ʮݱ৔Ͱͷʢ͍ۤʣܦݧʯΛ౿·͑ͯॻ͘ • ࣄ࣮ͷྻڍͰͳࣗ͘෼ͨͪͷࢥ૝Λॻ͘ • ৽چͷJavaίʔυΛฒ΂ͯܝࡌ͢Δ • ຊ೔΋ࡏݿ͖࣋ͬͯͯ·͢ʂ • ͓஋ஈͳΜͱ3,000ԁʂʂʢఆՁɿ3,218ԁʣ • ʮͤΖ͞Μ͸Ͳ͜ॻ͍ͨͷʁʯ͸࣭໰ېࢭͰ͢ 3
  3. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 4 ݹ͍όʔδϣϯͷ+BWBͰ
 ॻ͍ͨίʔυΛ
 ࠷ۙͷόʔδϣϯͷ+BWBͷίʔυʹ
 ม͍͑ͯ͘ͱ͍͏͓࿩Ͱ͢
  4. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 5BCMFPGDPOUFOUT 1. Introduction 2. From 1.4 to 5.0 3. From 5.0 to 7 4. From 7 To 8 5
  5. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 7 ࠓճ঺հ͢Διʔείʔυ $47ϑΝΠϧͷಡΈॻ͖ *OUSPEVDUJPO
  6. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 8 *OUSPEVDUJPO CSV File Java App ௥ه ಡΈࠐΈ 2017-04-25,100,123456,Acroquest,څ༩ 2017-05-02,100,-28980,λχϞτ,Χʔυར༻ 2017-05-03,100,-17712,λχϞτ,Χʔυར༻ 2017-05-20,100,3000000,gihyo,ҹ੫ 2017-06-22,100,200000,ΧΰΠέ,ATMৼΓࠐΈ 2017-06-22,100,-1000000,λχϞτ,ATMҾ͖ग़͠ ʢۜߦͷར༻໌ࡉͷΠϝʔδʣ
 ೔෇,ޱ࠲൪߸,ֹۚ,ར༻ऀ,ࡉ໨
  7. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 9 ؆୯ʹιʔεݟͤ·͢ *OUSPEVDUJPO
  8. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 10 void append(String fileName, String[] items) { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < items.length; i++) { if (buffer.length() > 0) { buffer.append(","); } buffer.append(items[i]); } String row = buffer.toString() + '\n'; Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "UTF-8")); writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { // ignore } } } ϑΝΠϧ΁ͷCSVߦ௥Ճ
  9. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 11 StringBuffer buffer = new StringBuffer(); for (int i = 0; i < items.length; i++) { if (buffer.length() > 0) { buffer.append(","); } buffer.append(items[i]); } String row = buffer.toString() + '\n'; ϑΝΠϧ΁ͷCSVߦ௥Ճʢจࣈ഑ྻͷ݁߹ʣ
  10. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 12 Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(
 new FileOutputStream(fileName, true), "UTF-8")); writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { // ignore } } ϑΝΠϧ΁ͷCSVߦ௥ՃʢϑΝΠϧ΁ͷ௥هʣ Ϧιʔε͸finallyͰclose͢Δ
  11. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 13 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List list = new ArrayList(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx); } } catch (ParseException e) { throw new RuntimeException("failed to parse date", e); } catch (IOException e) { throw new RuntimeException("failed to read file: " + fileName, e); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // ignore } } return list; } ϑΝΠϧͷಡΈࠐΈ
  12. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 14 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List list = new ArrayList(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  13. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 15 } catch (ParseException e) { throw new RuntimeException("failed to parse date", e); } catch (IOException e) { throw new RuntimeException("failed to read file: " + fileName, e); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // ignore } } return list; } ϑΝΠϧͷಡΈࠐΈʢޙ൒ʣ Ϧιʔε͸finallyͰclose͢Δʢେࣄʣ
  14. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 16 class Transaction { Date date; String accountId; Integer amount; String name; String note; public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } public String toString() { return date + " " + accountId + " " + amount + " " + name + " " + note; } } TransactionΫϥε
  15. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 17 class Transaction { public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } public String toString() { return date + " " + accountId + " " + amount + " " + name + " " + note; } } TransactionΫϥεʢϝιουͷΈʣ
  16. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 18 String fileName = "./transactions.csv"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); append(fileName, new Transaction(df.parse("2017-04-25"), "100", 123456, "Acroquest", "給与")); append(fileName, new Transaction(df.parse("2017-05-02"), "100", -28980, "タニモト", "カード利用")); append(fileName, new Transaction(df.parse("2017-05-03"), "100", -17712, "タニモト", "カード利用")); append(fileName, new Transaction(df.parse("2017-05-20"), "100", 3000000, "gihyo", "印税")); append(fileName, new Transaction(df.parse("2017-06-22"), "100", 200000, "カゴイケ", "ATM振り込み")); append(fileName, new Transaction(df.parse("2017-06-22"), "100", -1000000, "タニモト", "ATM引き出し")); ར༻ଆͷྫ - σʔλͷ௥Ճ
  17. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 19 List list = read(fileName); int sum = 0; for (int i = 0; i < list.size(); i++) { Transaction tx = (Transaction) list.get(i); sum += tx.amount; } System.out.println(sum); ར༻ଆͷྫ - ࢒ߴͷܭࢉ
  18. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug *OUSPEVDUJPO 20 List list = read(fileName); for (int i = 0; i < list.size(); i++) { Transaction tx = (Transaction) list.get(i); Calendar cal = Calendar.getInstance(); cal.setTime(tx.date); if (cal.get(Calendar.MONTH) == 5) { System.out.println(tx); } } ར༻ଆͷྫ - 6݄෼ͷऔҾͷΈදࣔ ݄͸0͔Β࢝·Δ
  19. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 21 ͍·ݟͤͨίʔυ͸
 +BWBͰಈ͖·͢ *OUSPEVDUJPO
  20. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 24 (FOFSJDT 'SPNUP GPSFBDI WBSBSHT FOVN TUBUJDJNQPSU BVUPCPYJOH ʜBOENPSF
  21. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 25 +BWBจ๏ͷجૅ 'SPNUP
  22. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 26 จࣈ഑ྻͷ݁߹
 CFGPSFBGUFS 'SPNUP
  23. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 27 StringBuffer buffer = new StringBuffer(); for (int i = 0; i < items.length; i++) { if (buffer.length() > 0) { buffer.append(","); } buffer.append(items[i]); } String row = buffer.toString() + '\n'; จࣈ഑ྻͷ݁߹
  24. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 28 StringBuilder builder = new StringBuilder(); for (String item : items) { if (builder.length() > 0) { builder.append(","); } builder.append(item); } String row = builder.toString() + '\n'; จࣈ഑ྻͷ݁߹
  25. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 29 StringBuilder builder = new StringBuilder(); for (String item : items) { if (builder.length() > 0) { builder.append(","); } builder.append(item); } String row = builder.toString() + '\n'; จࣈ഑ྻͷ݁߹ StringBuilderͷํ͕
 ཧ࿦తʹੑೳ͕ྑ͍ foreachจʢ֦ுforจʣͰ
 ϧʔϓΛ؆ܿʹॻ͘
  26. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 30 ϑΝΠϧಡΈࠐΈ
 CFGPSFBGUFS 'SPNUP
  27. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 31 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List list = new ArrayList(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  28. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 32 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<Transaction>(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  29. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 33 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<Transaction>(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ GenericsͰܕΛ໌֬ʹ
  30. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 34 List list = read(fileName); int sum = 0; for (int i = 0; i < list.size(); i++) { Transaction tx = (Transaction) list.get(i); sum += tx.amount; } System.out.println(sum); ར༻ଆͷྫ - ࢒ߴͷܭࢉ
  31. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 35 List<Transaction> list = read(fileName); int sum = 0; for (Transaction tx : list) { sum += tx.amount; } System.out.println(sum); ར༻ଆͷྫ - ࢒ߴͷܭࢉ
  32. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 36 List<Transaction> list = read(fileName); int sum = 0; for (Transaction tx : list) { sum += tx.amount; } System.out.println(sum); ར༻ଆͷྫ - ࢒ߴͷܭࢉ ΩϟετʢܕΛ໌ࣔʣΛͤͣʹ
 ར༻Ͱ͖Δ
  33. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 37 7BMVF0CKFDU
 CFGPSFBGUFS 'SPNUP
  34. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 38 class Transaction { public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } public String toString() { return date + " " + accountId + " " + amount + " " + name + " " + note; } } TransactionΫϥεʢϝιουͷΈʣ
  35. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 39 class Transaction { public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } @Override public String toString() { return date + " " + accountId + " " + amount + " " + name + " " + note; } } TransactionΫϥεʢϝιουͷΈʣ
  36. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 40 class Transaction { public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } @Override public String toString() { return date + " " + accountId + " " + amount + " " + name + " " + note; } } TransactionΫϥεʢϝιουͷΈʣ ϝιουͷܧঝʢ࣮૷ʣͰ͋Δ͜ͱΛ
 ໌ࣔͯ͠ϛεΛ๷͙
  37. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 41 ͕͢͞ʹ͜ͷล͸
 େৎ෉Ͱ͢ΑͶ☺ 'SPNUP
  38. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 44 USZXJUISFTPVSDFT 'SPNUP EJBNPOEPQFSBUPS OJP NVMUJDBUDI ʜBOENPSF
  39. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 45 ͜ͷลΓ͔Βগ͠ཧղ͕
 ո͘͠ͳΔਓ͕͍Δ 'SPNUP
  40. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 46 ϑΝΠϧॻ͖ࠐΈ
 CFGPSFBGUFS 'SPNUP
  41. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 47 Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(
 new FileOutputStream(fileName, true), "UTF-8")); writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { // ignore } } ϑΝΠϧ΁ͷ௥ه
  42. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 48 try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), StandardCharsets.UTF_8))) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه
  43. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 49 try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), StandardCharsets.UTF_8))) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه try-with-resourecesʹΑΔ
 ϦιʔεͷࣗಈΫϩʔζ
  44. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 50 pOBMMZͰDMPTF͢Δͷ͸
 ݹ͍ఆੴ 'SPNUP
  45. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 51 try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), StandardCharsets.UTF_8))) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه try/catch͔Βൈ͚ͨ࣌ʹ ( ) Ͱએݴ͍ͯͨ͠Ϧιʔε͕close͞ΕΔɻ
 ( ) ͷதͰએݴͰ͖Δม਺͸AutoCloseableΠϯλϑΣʔεͷΫϥεͷΈɻ
  46. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 52 try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), StandardCharsets.UTF_8))) { writer.write(row); } ϑΝΠϧ΁ͷ௥ه catchͤͣʹϝιουʹ throws IOExceptionΛ͚ͭΔ࣌Ͱ΋
 try-with-resourcesʹ͓͚ͯ͠͹ɺ͖ͪΜͱclose͞ΕΔɻ
  47. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 53 try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileName, true), StandardCharsets.UTF_8))) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه ϑΝΠϧΤϯίʔυΛStringͰ͸ͳ͘CharsetͰࢦఆ͢ΔͨΊʹ
 ୅දతͳΤϯίʔυΛྻڍ͢ΔStandardCharsetsΛར༻͢Δɻ
 Τϯίʔυ͕ݟ͔ͭΒͳ͍࣌ͷྫ֎ൃੜΛ๷͛Δɻ
  48. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 54 ͞Βʹ
 ϑΝΠϧॻ͖ࠐΈ
 BGUFS 'SPNUP
  49. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 55 try (Writer writer = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه
  50. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 56 try (Writer writer = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه ϑΝΠϧͷૢ࡞΍Ϧιʔεͷ࡞੒Λ؆ܿʹॻ͚Δ
 FilesΫϥε͕௥Ճʢnio2ʣ
  51. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 57 try (Writer writer = Files.newBufferedWriter(Paths.get(fileName), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) { writer.write(row); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه OptionͷࢦఆͰॻ͖ࠐΈͷϧʔϧΛࢦఆɻ
 ͜ͷྫͰ͸࣍ͷΦϓγϣϯΛࢦఆ
 ɹɾϑΝΠϧ͕ͳ͚Ε͹৽ن࡞੒ɺطʹ͋Ε͹ͦΕΛ࢖͏
 ɹɾϑΝΠϧʹ͸௥ه͢Δʢ্ॻ͖Ͱ͸ͳ͍ʣ
  52. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 58 ͞Βʹ͞Βʹ
 ϑΝΠϧॻ͖ࠐΈ
 BGUFS 'SPNUP
  53. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 59 String row = builder.toString(); try { List<String> rows = Collections.singletonList(row); Files.write(Paths.get(fileName), rows, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه
  54. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 60 String row = builder.toString(); try { List<String> rows = Collections.singletonList(row); Files.write(Paths.get(fileName), rows, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه FilesͷwriteϝιουͰɺϦετʢIterableʣΛ
 ·ͱΊͯϑΝΠϧʹॻ͖ࠐΈՄೳɻ
 ͳ͓ߦ຤ʹ͸վߦίʔυ͕ೖΔɻ
  55. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 61 ϑΝΠϧಡΈࠐΈ
 CFGPSFBGUFS 'SPNUP
  56. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 62 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<Transaction>(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(
 new FileInputStream(fileName), "UTF-8")); String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  57. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 63 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<>(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  58. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 64 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<>(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ μΠΞϞϯυԋࢉࢠͰ
 هड़Λ؆ܿʹ try-with-resourcesͱɺFilesͷϦιʔεੜ੒ɺ
 StandardCharsets͸΋͏ֶͼ·ͨ͠
  59. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 65 } catch (ParseException e) { throw new RuntimeException("failed to parse date", e); } catch (IOException e) { throw new RuntimeException("failed to read file: " + fileName, e); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // ignore } } return list; } ϑΝΠϧͷಡΈࠐΈʢޙ൒ʣ
  60. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 66 } catch (ParseException | IOException e) { throw new RuntimeException("failed to parse date or read file”, e); } return list; } ϑΝΠϧͷಡΈࠐΈʢޙ൒ʣ ෳ਺ͷྫ֎Λ·ͱΊͯcatchͰ͖Δ
 ʢຊདྷ͸ɺྫ֎ॲཧͷ಺༰͕શ͘ಉ࣌͡ʹ࢖͏ʣ
  61. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 67 USZXJUISFTPVSDFT͸
 ͱʹ͔͘ඇৗʹେ੾ͳػೳ 'SPNUP
  62. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 69 4USFBN"1* 'SPNUP -BNCEB %BUFBOE5JNF"1* ʜBOENPSF
  63. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 70 4USFBN"1*ͱ-BNCEBࣜͰ
 Ұؾʹจ๏͕มΘͬͨ 'SPNUP
  64. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 71 ɾɾɾ͚ͲɺͦΕ͸ޙʹͯ͠
 ͦΕҎ֎ͷݟམͱ͞Ε͕ͪͳ
 ͱ͜Ζ͔Β΍Γ·͢ 'SPNUP
  65. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 72 จࣈ഑ྻͷ݁߹ͱ
 ϑΝΠϧॻ͖ࠐΈ
 CFGPSFBGUFS 'SPNUP
  66. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 73 StringBuilder builder = new StringBuilder(); for (String item : items) { if (builder.length() > 0) { builder.append(","); } builder.append(item); } String row = builder.toString() + '\n'; try { List<String> rows = Collections.singletonList(row); Files.write(Paths.get(fileName), rows, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه
  67. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 74 String row = String.join(",", items); try { List<String> rows = Collections.singletonList(row); Files.write(Paths.get(fileName), rows, // StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه
  68. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 75 String row = String.join(",", items); try { List<String> rows = Collections.singletonList(row); Files.write(Paths.get(fileName), rows, // StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (IOException e) { throw new RuntimeException("unable to write file: " + fileName, e); } ϑΝΠϧ΁ͷ௥ه ͱ͏ͱ͏join͕Մೳʹʂ ϑΝΠϧΤϯίʔυࢦఆͷলུ͕Մೳɻ
 লུͨ͠৔߹͸UTF-8ͱͳΔɻ
  69. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 76 ೔෇ΛؚΉ7BMVF0CKFDU
 CFGPSFBGUFS 'SPNUP
  70. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 77 class Transaction { Date date; String accountId; Integer amount; String name; String note; public Transaction() { } public Transaction(Date date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } TransactionΫϥεʢtoStringলུʣ
  71. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 78 class Transaction { LocalDate date; String accountId; Integer amount; String name; String note; public Transaction() { } public Transaction(LocalDate date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } TransactionΫϥεʢtoStringলུʣ
  72. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 79 class Transaction { LocalDate date; String accountId; Integer amount; String name; String note; public Transaction() { } public Transaction(LocalDate date, String accountId, Integer amount, String name, String note) { this.date = date; this.accountId = accountId; this.amount = amount; this.name = name; this.note = note; } TransactionΫϥεʢtoStringলུʣ LocalDateͰ೔෇ͷΈΛѻ͏ɻ
 ଞʹ΋LocalDateTime΍LocalTimeɺ
 OffsetDateTimeɺZonedDateTimeͳͲ͋Γ
  73. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 80 List<Transaction> read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<>(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName), StandardCharsets.UTF_8)) { String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = df.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  74. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 81 List<Transaction> read(String fileName) { // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<>(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) { String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = LocalDate.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ
  75. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 'SPNUP 82 List<Transaction> read(String fileName) { // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List<Transaction> list = new ArrayList<>(); try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) { String line; while ((line = reader.readLine()) != null) { String[] s = line.split(","); Transaction tx = new Transaction(); tx.date = LocalDate.parse(s[0]); tx.accountId = s[1]; tx.amount = Integer.valueOf(s[2]); tx.name = s[3]; tx.note = s[4]; list.add(tx);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ ϋΠϑϯ۠੾Γͷ
 ISO 8601ܗࣜͷ೔࣌ͳΒ
 ϑΥʔϚολͳ͠ͰύʔεՄೳ ΤϯίʔυΛলུ͢Δͱ
 UTF-8ͱͳΔ
  76. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug String fileName = "./transactions.csv"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); append(fileName, new Transaction(df.parse("2017-04-25"), "100", 123456, "Acroquest", "給与")); append(fileName, new Transaction(df.parse("2017-05-02"), "100", -28980, "タニモト", "カード利用")); append(fileName, new Transaction(df.parse("2017-05-03"), "100", -17712, "タニモト", "カード利用")); append(fileName, new Transaction(df.parse("2017-05-20"), "100", 3000000, "gihyo", "印税")); append(fileName, new Transaction(df.parse("2017-06-22"), "100", 200000, "カゴイケ", "ATM振り込み")); append(fileName, new Transaction(df.parse("2017-06-22"), "100", -1000000, "タニモト", "ATM引き出し")); 83 ར༻ଆͷྫ - σʔλͷ௥Ճ 'SPNUP
  77. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 84 String fileName = "./transactions.csv"; // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); append(fileName, new Transaction(LocalDate.of(2017, 4, 25), "100", 123456, "Acroquest", "給与")); append(fileName, new Transaction(LocalDate.of(2017, 5, 2), "100", -28980, "タニモト", "カード利用")); append(fileName, new Transaction(LocalDate.of(2017, 5, 3), "100", -17712, "タニモト", "カード利用")); append(fileName, new Transaction(LocalDate.of(2017, 5, 20), "100", 3000000, "gihyo", "印税")); append(fileName, new Transaction(LocalDate.of(2017, 6, 22), "100", 200000, "カゴイケ", "ATM振り込み")); append(fileName, new Transaction(LocalDate.of(2017, 6, 22), "100", -1000000, "タニモト", “ATM引き出し")); ར༻ଆͷྫ - σʔλͷ௥Ճ 'SPNUP
  78. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 85 String fileName = "./transactions.csv"; // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); append(fileName, new Transaction(LocalDate.of(2017, 4, 25), "100", 123456, "Acroquest", "給与")); append(fileName, new Transaction(LocalDate.of(2017, 5, 2), "100", -28980, "タニモト", "カード利用")); append(fileName, new Transaction(LocalDate.of(2017, 5, 3), "100", -17712, "タニモト", "カード利用")); append(fileName, new Transaction(LocalDate.of(2017, 5, 20), "100", 3000000, "gihyo", "印税")); append(fileName, new Transaction(LocalDate.of(2017, 6, 22), "100", 200000, "カゴイケ", "ATM振り込み")); append(fileName, new Transaction(LocalDate.of(2017, 6, 22), "100", -1000000, "タニモト", “ATM引き出し")); ར༻ଆͷྫ - σʔλͷ௥Ճ ೥ɺ݄ɺ೔ɺ࣌ɺ෼ɺඵͳͲΛࢦఆͯ͠
 Πϯελϯεੜ੒͕Մೳɻ
 ͔͠΋݄͸1͔Β࢝·Δʂ 'SPNUP
  79. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 86 ೔෇ܭࢉ
 CFGPSFBGUFS 'SPNUP
  80. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 87 List<Transaction> list = read(fileName); for (Transaction tx : list) { Calendar cal = Calendar.getInstance(); cal.setTime(tx.date); if (cal.get(Calendar.MONTH) == 5) { System.out.println(tx); } } ར༻ଆͷྫ - 6݄෼ͷऔҾͷΈදࣔ 'SPNUP
  81. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 88 List<Transaction> list = read(fileName); for (Transaction tx : list) { Calendar cal = Calendar.getInstance(); cal.setTime(tx.date); if (cal.get(Calendar.MONTH) == 5) { System.out.println(tx); } } ར༻ଆͷྫ - 6݄෼ͷऔҾͷΈදࣔ 'SPNUP ݄͸0͔Β࢝·ΔͷͰɺ6݄͸5ʹͳΔɻ
 ʢCalendar.JUNEͱ͍͏ఆ਺΋ར༻Մೳʣ
  82. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 89 List<Transaction> list = read(fileName); for (Transaction tx : list) { if (tx.date.getMonth() == Month.JUNE) { System.out.println(tx); } } ར༻ଆͷྫ - 6݄෼ͷऔҾͷΈදࣔ 'SPNUP
  83. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 90 List<Transaction> list = read(fileName); for (Transaction tx : list) { if (tx.date.getMonth() == Month.JUNE) { System.out.println(tx); } } ར༻ଆͷྫ - 6݄෼ͷऔҾͷΈදࣔ 'SPNUP ݄͸enumͱͯ͠ఆٛ͞Ε͓ͯΓ
 enumಉ࢜ͷൺֱͰ൑ఆ͢Δ
  84. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 91 4USFBN-BNCEBͷ
 ӨʹӅΕ͕͚ͪͩͲ
 ༗༻ͳ"1*͕૿͍͑ͯΔ 'SPNUP
  85. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 92 Ͱɺ؊৺ͷ
 4USFBN"1*ͱ-BNCEB
 ͸ɾɾɾʁ 'SPNUP
  86. Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t

    #jjug 94 Ξϯέʔτޚڠྗ͓ئ͍͠·͢
 http://bit.ly/jjug201706