Share
Embed
from old Java to modern Java (2017) #jjug
JJUG ナイトセミナー 「Java エンジニアのためのJava(再)入門」
https://jjug.doorkeeper.jp/events/61857
Shin Tanimoto
June 28, 2017
Transcript
94 slides
-
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 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug ॻ੶Λॻ͖·ͨ͠ • Javaຊ֨ೖ େධൃചதʂ • ॻ࣌͘ʹେʹͨ͜͠ͱ • ʮݱͰͷʢ͍ۤʣܦݧʯΛ౿·͑ͯॻ͘ • ࣄ࣮ͷྻڍͰͳࣗͨͪ͘ͷࢥΛॻ͘ • ৽چͷJavaίʔυΛฒͯܝࡌ͢Δ • ຊࡏݿ͖࣋ͬͯͯ·͢ʂ • ͓ஈͳΜͱ3,000ԁʂʂʢఆՁɿ3,218ԁʣ • ʮͤΖ͞ΜͲ͜ॻ͍ͨͷʁʯ࣭ېࢭͰ͢ 3 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 4 ݹ͍όʔδϣϯͷ+BWBͰ ॻ͍ͨίʔυΛ ࠷ۙͷόʔδϣϯͷ+BWBͷίʔυʹ ม͍͑ͯ͘ͱ͍͏͓Ͱ͢ -
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 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug #1 Introduction 6 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 7 ࠓճհ͢Διʔείʔυ $47ϑΝΠϧͷಡΈॻ͖ *OUSPEVDUJPO -
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Ҿ͖ग़͠ ʢۜߦͷར༻໌ࡉͷΠϝʔδʣ ,ޱ࠲൪߸,ֹۚ,ར༻ऀ,ࡉ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 9 ؆୯ʹιʔεݟͤ·͢ *OUSPEVDUJPO -
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ߦՃ -
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ߦՃʢจࣈྻͷ݁߹ʣ -
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͢Δ -
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; } ϑΝΠϧͷಡΈࠐΈ -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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͢Δʢେࣄʣ -
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Ϋϥε -
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ΫϥεʢϝιουͷΈʣ -
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引き出し")); ར༻ଆͷྫ - σʔλͷՃ -
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); ར༻ଆͷྫ - ߴͷܭࢉ -
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͔Β࢝·Δ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 21 ͍·ݟͤͨίʔυ +BWBͰಈ͖·͢ *OUSPEVDUJPO -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug #2 From 1.4 to 5.0 22 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 23 +BWB4& 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 24 (FOFSJDT 'SPNUP GPSFBDI WBSBSHT FOVN TUBUJDJNQPSU BVUPCPYJOH ʜBOENPSF -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 25 +BWBจ๏ͷجૅ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 26 จࣈྻͷ݁߹ CFGPSFBGUFS 'SPNUP -
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'; จࣈྻͷ݁߹ -
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'; จࣈྻͷ݁߹ -
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จʣͰ ϧʔϓΛ؆ܿʹॻ͘ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 30 ϑΝΠϧಡΈࠐΈ CFGPSFBGUFS 'SPNUP -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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ͰܕΛ໌֬ʹ -
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); ར༻ଆͷྫ - ߴͷܭࢉ -
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); ར༻ଆͷྫ - ߴͷܭࢉ -
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); ར༻ଆͷྫ - ߴͷܭࢉ ΩϟετʢܕΛ໌ࣔʣΛͤͣʹ ར༻Ͱ͖Δ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 37 7BMVF0CKFDU CFGPSFBGUFS 'SPNUP -
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ΫϥεʢϝιουͷΈʣ -
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ΫϥεʢϝιουͷΈʣ -
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ΫϥεʢϝιουͷΈʣ ϝιουͷܧঝʢ࣮ʣͰ͋Δ͜ͱΛ ໌ࣔͯ͠ϛεΛ͙ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 41 ͕͢͞ʹ͜ͷล େৎͰ͢ΑͶ☺ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug #3 From 5.0 to 7 42 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 43 +BWB4& 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 44 USZXJUISFTPVSDFT 'SPNUP EJBNPOEPQFSBUPS OJP NVMUJDBUDI ʜBOENPSF -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 45 ͜ͷลΓ͔Βগ͠ཧղ͕ ո͘͠ͳΔਓ͕͍Δ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 46 ϑΝΠϧॻ͖ࠐΈ CFGPSFBGUFS 'SPNUP -
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 } } ϑΝΠϧͷه -
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); } ϑΝΠϧͷه -
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ʹΑΔ ϦιʔεͷࣗಈΫϩʔζ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 50 pOBMMZͰDMPTF͢Δͷ ݹ͍ఆੴ 'SPNUP -
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ΠϯλϑΣʔεͷΫϥεͷΈɻ -
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͞ΕΔɻ -
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Λར༻͢Δɻ Τϯίʔυ͕ݟ͔ͭΒͳ͍࣌ͷྫ֎ൃੜΛ͛Δɻ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 54 ͞Βʹ ϑΝΠϧॻ͖ࠐΈ BGUFS 'SPNUP -
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); } ϑΝΠϧͷه -
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ʣ -
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ͷࢦఆͰॻ͖ࠐΈͷϧʔϧΛࢦఆɻ ͜ͷྫͰ࣍ͷΦϓγϣϯΛࢦఆ ɹɾϑΝΠϧ͕ͳ͚Ε৽ن࡞ɺطʹ͋ΕͦΕΛ͏ ɹɾϑΝΠϧʹه͢Δʢ্ॻ͖Ͱͳ͍ʣ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 58 ͞Βʹ͞Βʹ ϑΝΠϧॻ͖ࠐΈ BGUFS 'SPNUP -
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); } ϑΝΠϧͷه -
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ʣΛ ·ͱΊͯϑΝΠϧʹॻ͖ࠐΈՄೳɻ ͳ͓ߦʹվߦίʔυ͕ೖΔɻ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 61 ϑΝΠϧಡΈࠐΈ CFGPSFBGUFS 'SPNUP -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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͏ֶͼ·ͨ͠ -
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; } ϑΝΠϧͷಡΈࠐΈʢޙʣ -
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Ͱ͖Δ ʢຊདྷɺྫ֎ॲཧͷ༰͕શ͘ಉ࣌͡ʹ͏ʣ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 67 USZXJUISFTPVSDFT ͱʹ͔͘ඇৗʹେͳػೳ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug #4 From 7 to 8 68 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 69 4USFBN"1* 'SPNUP -BNCEB %BUFBOE5JNF"1* ʜBOENPSF -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 70 4USFBN"1*ͱ-BNCEBࣜͰ Ұؾʹจ๏͕มΘͬͨ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 71 ɾɾɾ͚ͲɺͦΕޙʹͯ͠ ͦΕҎ֎ͷݟམͱ͞Ε͕ͪͳ ͱ͜Ζ͔ΒΓ·͢ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 72 จࣈྻͷ݁߹ͱ ϑΝΠϧॻ͖ࠐΈ CFGPSFBGUFS 'SPNUP -
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); } ϑΝΠϧͷه -
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); } ϑΝΠϧͷه -
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ͱͳΔɻ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 76 ΛؚΉ7BMVF0CKFDU CFGPSFBGUFS 'SPNUP -
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লུʣ -
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লུʣ -
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ͰͷΈΛѻ͏ɻ ଞʹLocalDateTimeLocalTimeɺ OffsetDateTimeɺZonedDateTimeͳͲ͋Γ -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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); } ϑΝΠϧͷಡΈࠐΈʢલʣ -
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ͱͳΔ -
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 -
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 -
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 -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 86 ܭࢉ CFGPSFBGUFS 'SPNUP -
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 -
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ͱ͍͏ఆར༻Մೳʣ -
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 -
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ಉ࢜ͷൺֱͰఆ͢Δ -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 91 4USFBN-BNCEBͷ ӨʹӅΕ͕͚ͪͩͲ ༗༻ͳ"1*͕૿͍͑ͯΔ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 92 Ͱɺ؊৺ͷ 4USFBN"1*ͱ-BNCEB ɾɾɾʁ 'SPNUP -
Copyright © Acroquest Technology Co., Ltd. All rights reserved. @cero_t
#jjug 94 Ξϯέʔτޚڠྗ͓ئ͍͠·͢ http://bit.ly/jjug201706