Slide 1

Slide 1 text

GSPNPME+BWBUPNPEFSO+BWB
 +BWBॳ৺ऀ͔Β࣍ͷεςοϓ΁
 ͋Δ͍͸ࡀ͔Βͷ+BWB࠶ೖ໳
 #jjug Acroquest Technology Co., LTD.
 Shin Tanimoto (@cero_t)

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug #1
 Introduction 6

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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Ҿ͖ग़͠ ʢۜߦͷར༻໌ࡉͷΠϝʔδʣ
 ೔෇,ޱ࠲൪߸,ֹۚ,ར༻ऀ,ࡉ໨

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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ߦ௥Ճ

Slide 11

Slide 11 text

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ߦ௥Ճʢจࣈ഑ྻͷ݁߹ʣ

Slide 12

Slide 12 text

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͢Δ

Slide 13

Slide 13 text

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; } ϑΝΠϧͷಡΈࠐΈ

Slide 14

Slide 14 text

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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 15

Slide 15 text

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͢Δʢେࣄʣ

Slide 16

Slide 16 text

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Ϋϥε

Slide 17

Slide 17 text

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ΫϥεʢϝιουͷΈʣ

Slide 18

Slide 18 text

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引き出し")); ར༻ଆͷྫ - σʔλͷ௥Ճ

Slide 19

Slide 19 text

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); ར༻ଆͷྫ - ࢒ߴͷܭࢉ

Slide 20

Slide 20 text

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͔Β࢝·Δ

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug #2 From 1.4 to 5.0 22

Slide 23

Slide 23 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 23 +BWB4& 'SPNUP

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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'; จࣈ഑ྻͷ݁߹

Slide 28

Slide 28 text

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'; จࣈ഑ྻͷ݁߹

Slide 29

Slide 29 text

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จʣͰ
 ϧʔϓΛ؆ܿʹॻ͘

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 32

Slide 32 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 32 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 33

Slide 33 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 33 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ GenericsͰܕΛ໌֬ʹ

Slide 34

Slide 34 text

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); ར༻ଆͷྫ - ࢒ߴͷܭࢉ

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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ΫϥεʢϝιουͷΈʣ

Slide 39

Slide 39 text

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ΫϥεʢϝιουͷΈʣ

Slide 40

Slide 40 text

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ΫϥεʢϝιουͷΈʣ ϝιουͷܧঝʢ࣮૷ʣͰ͋Δ͜ͱΛ
 ໌ࣔͯ͠ϛεΛ๷͙

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug #3 From 5.0 to 7 42

Slide 43

Slide 43 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 43 +BWB4& 'SPNUP

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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 } } ϑΝΠϧ΁ͷ௥ه

Slide 48

Slide 48 text

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); } ϑΝΠϧ΁ͷ௥ه

Slide 49

Slide 49 text

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ʹΑΔ
 ϦιʔεͷࣗಈΫϩʔζ

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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ΠϯλϑΣʔεͷΫϥεͷΈɻ

Slide 52

Slide 52 text

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͞ΕΔɻ

Slide 53

Slide 53 text

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Λར༻͢Δɻ
 Τϯίʔυ͕ݟ͔ͭΒͳ͍࣌ͷྫ֎ൃੜΛ๷͛Δɻ

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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); } ϑΝΠϧ΁ͷ௥ه

Slide 56

Slide 56 text

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ʣ

Slide 57

Slide 57 text

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ͷࢦఆͰॻ͖ࠐΈͷϧʔϧΛࢦఆɻ
 ͜ͷྫͰ͸࣍ͷΦϓγϣϯΛࢦఆ
 ɹɾϑΝΠϧ͕ͳ͚Ε͹৽ن࡞੒ɺطʹ͋Ε͹ͦΕΛ࢖͏
 ɹɾϑΝΠϧʹ͸௥ه͢Δʢ্ॻ͖Ͱ͸ͳ͍ʣ

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 59 String row = builder.toString(); try { List 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); } ϑΝΠϧ΁ͷ௥ه

Slide 60

Slide 60 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 60 String row = builder.toString(); try { List 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ʣΛ
 ·ͱΊͯϑΝΠϧʹॻ͖ࠐΈՄೳɻ
 ͳ͓ߦ຤ʹ͸վߦίʔυ͕ೖΔɻ

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 62 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 63

Slide 63 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 63 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 64

Slide 64 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 64 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List 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͸΋͏ֶͼ·ͨ͠

Slide 65

Slide 65 text

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; } ϑΝΠϧͷಡΈࠐΈʢޙ൒ʣ

Slide 66

Slide 66 text

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Ͱ͖Δ
 ʢຊདྷ͸ɺྫ֎ॲཧͷ಺༰͕શ͘ಉ࣌͡ʹ࢖͏ʣ

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug #4 From 7 to 8 68

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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 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); } ϑΝΠϧ΁ͷ௥ه

Slide 74

Slide 74 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 74 String row = String.join(",", items); try { List 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); } ϑΝΠϧ΁ͷ௥ه

Slide 75

Slide 75 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 75 String row = String.join(",", items); try { List 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ͱͳΔɻ

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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লུʣ

Slide 78

Slide 78 text

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লུʣ

Slide 79

Slide 79 text

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ͳͲ͋Γ

Slide 80

Slide 80 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 80 List read(String fileName) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 81

Slide 81 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 81 List read(String fileName) { // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List 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);
 } ϑΝΠϧͷಡΈࠐΈʢલ൒ʣ

Slide 82

Slide 82 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 'SPNUP 82 List read(String fileName) { // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); List 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ͱͳΔ

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 87 List 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

Slide 88

Slide 88 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 88 List 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ͱ͍͏ఆ਺΋ར༻Մೳʣ

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 93 ޙ൒ʹͭͮ͘ʂ

Slide 94

Slide 94 text

Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t #jjug 94 Ξϯέʔτޚڠྗ͓ئ͍͠·͢
 http://bit.ly/jjug201706