{"id":693,"date":"2019-12-30T23:07:46","date_gmt":"2019-12-30T14:07:46","guid":{"rendered":"https:\/\/www.itchefblog.com\/?p=693"},"modified":"2019-12-30T23:07:46","modified_gmt":"2019-12-30T14:07:46","slug":"kotlin-file-io","status":"publish","type":"post","link":"https:\/\/www.itchefblog.com\/?p=693","title":{"rendered":"Kotlin File IO"},"content":{"rendered":"<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\ud30c\uc77c \uc4f0\uae30 1\r\nimport java.io.IOException\r\nimport java.nio.file.Files\r\nimport java.nio.file.Paths\r\nimport java.nio.file.StandardOpenOption\r\n\r\nfun main(){\r\n    val path = &quot;\/file\/path\/hello.txt&quot;\r\n    val text = &quot;\uc548\ub155\uc548\ub155\uc548\ub155\uc548\ub155n\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c&quot;\r\n\r\n    try {\r\n        Files.write(Paths.get(path), text.toByteArray(), StandardOpenOption.CREATE)\r\n    }catch(e: IOException){\r\n\t\t\t\t\/\/Exception \ucc98\ub9ac\r\n    }\r\n}\r\n\r\n\/\/standardOpenOption\uc758 \uc635\uc158\uc740 CREATE, APPEND, WRITE, READ\uac00 \uc788\ub2e4.\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\ud30c\uc77c \uc4f0\uae30 2\r\nimport java.io.File\r\nimport java.io.FileWriter\r\nimport java.io.PrintWriter\r\n\r\nfun main(){\r\n    val outString: String = &quot;\uc548\ub155\ud558\uc2e0\uac00?tWhat are you doing now?nAre you Serious??rStrawberies!!&quot;\r\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/testfile.txt&quot;\r\n\r\n    val file = File(path)\r\n    val printWriter = PrintWriter(file)\r\n\r\n    printWriter.println(outString)\r\n    printWriter.close()\r\n\r\n\t\t\/\/\uc774\uac83\ub3c4 \uac19\uace0\r\n    File(path).printWriter().use{ out -&gt; out.println(outString) }\r\n\r\n\t\t\/\/\uc774\uac83\ub3c4 \uac19\ub2e4. \uc778\uc790\uac00 \ud558\ub098\uc774\uae30\uc5d0 \uc774\ub807\uac8c\uae4c\uc9c0 \uc904\uc77c \uc218 \uc788\ub2e4.\r\n    File(path).printWriter().use{ it.println(outString) }\r\n}\r\n\r\n\/\/\ucf54\ud2c0\ub9b0 \ud45c\uc900 \ud568\uc218\uc778 use\ub97c \uc0ac\uc6a9\ud558\uba74 \ub78c\ub2e4\uc2dd\uc73c\ub85c \uc804\ub2ec \ubc1b\uc740 \uac1c\uccb4\uac00 \uc0ac\uc6a9\ub41c \ub4a4 \uc790\ub3d9\uc73c\ub85c \uc548\uc804\ud558\uac8c \ud30c\uc77c\uc744 \ub2eb\uc544\uc900\ub2e4.\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\ud30c\uc77c \uc4f0\uae30 3\r\nval file = File(path)\r\nfile.writeText(outString)\r\nfile.appendText(&quot;nDo Great Work!&quot;)\r\n\r\nval writer = FileWriter(path, true)\r\ntry{\r\n    writer.write(outString)\r\n}catch(e: Exception){\r\n    \/\/\uc624\ub958 \ubc1c\uc0dd!\r\n}finally{\r\n    writer.close()\r\n}\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/\ucf54\ud2c0\ub9b0 \ucc45 493\ud398\uc774\uc9c0 \ucc38\uc870\r\n\/\/\ud30c\uc77c \uc77d\uae30 1\r\nimport java.io.*\r\n\r\nfun main(){\r\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/otr.txt&quot;\r\n\r\n    try{\r\n        val read = FileReader(path)\r\n        println(read.readText())\r\n    }catch(e: Exception){\r\n        println(e.message)\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush: kotlin; title: ; notranslate\" title=\"\">\r\n\/\/JAVA\uc2dd \ud30c\uc77c\uc77d\uae30 KOTLIN\uc73c\ub85c \ubcc0\uacbd\r\nimport java.io.*\r\nimport java.lang.Exception\r\n\r\nfun main(){\r\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/otr.txt&quot;\r\n\r\n    \/\/\ub2e8\uc21c\ubcc0\ud658\r\n    val file = File(path)\r\n    val inputStream: InputStream = file.inputStream()\r\n    val inputStreamReader = InputStreamReader(inputStream)\r\n    val sb = StringBuilder()\r\n    var line:  String?\r\n    val br = BufferedReader(inputStreamReader)\r\n    try{\r\n        line = br.readLine()\r\n        while(line != null){\r\n            sb.append(line, 'n')\r\n            line = br.readLine()\r\n        }\r\n        println(sb)\r\n    }catch(e: Exception){\r\n\r\n    }finally{\r\n        br.close()\r\n    }\r\n\r\n}\r\n\r\n\/\/\uc2e4\ud589\ud558\ub294\ub370 \ubb38\uc81c\ub294 \uc5c6\uc9c0\ub9cc \ub2e8\uc21c\ud788 \ucf54\ub4dc\ub97c \ucf54\ud2c0\ub9b0\uc5d0 \ub300\uc751\ud558\ub3c4\ub85d \ubc14\uafe8\uae30\uc5d0 \ubcf4\uae30\uc5d0\ub3c4 \uc88b\uc9c0 \uc54a\uace0 \ucf54\ub4dc\ub3c4 \uc5ec\uc804\ud788 \uae38\ub2e4.\r\n\r\n\/\/\uc644\uc804 \ucf54\ud2c0\ub9b0\uc2dd\uc73c\ub85c \ubcc0\ud658!\r\nval file = File(path)\r\nval inputStream: InputStream = file.inputStream()\r\nval text = inputStream.bufferedReader().use { it.readText() }\r\nprintln(text)\r\n\r\n\/\/Java\uc758 InputStream\uc5d0\ub294 bufferedReader\uac00 \uc5c6\uc9c0\ub9cc,\r\n\/\/\ucf54\ud2c0\ub9b0\uc758 kotlin.io \ub77c\uc774\ube0c\ub7ec\ub9ac \ud328\ud0a4\uc9c0\uc5d0\uc11c \ud655\uc7a5 \ud568\uc218 \uae30\ubc95\uc73c\ub85c InputStream\uc5d0 \ucd94\uac00\ub418\uc5c8\ub2e4.\r\n\r\n\/\/file\uac1d\uccb4\ub97c \uc0dd\ub7b5\ud558\uace0 \uc774\ub807\uac8c\ub3c4 \uc218\uc815 \uac00\ub2a5\r\nval bufferedReader: BufferedReader = File(path).bufferedReader()\r\nval inputString = bufferedReader.use { it.readText() }\r\nprintln(inputString)\r\n\r\n\/\/\uc904 \ub2e8\uc704\ub85c \ucc98\ub9ac\ud558\ub824\uba74 usd() \ub300\uc2e0 useLines()\ub97c \uc0ac\uc6a9 \uac00\ub2a5. \ucf54\ud2c0\ub9b0\uc758 \uc2dc\ud000\uc2a4\ub97c \uc774\uc6a9\ud558\uace0 \uc788\uc73c\uba74 \uc0ac\uc6a9 \ud6c4 \ud30c\uc77c\uc744 \uc790\ub3d9\uc73c\ub85c \ub2eb\ub294\ub2e4.\r\nval bufferedReader = File(path).bufferedReader()\r\nval lineList = mutableListOf&lt;String&gt;()\r\nbufferedReader.useLines { lines -&gt; lines.forEach { lineList.add(it) } }\r\nlineList.forEach { println(&quot;&gt; &quot; + it) }\r\n\r\n\/\/\uc774\uc81c bufferedReader\uae4c\uc9c0 \uc0dd\ub7b5\ud558\uace0 \ud30c\uc77c\uc744 \uc9c1\uc811 \uc0ac\uc6a9\ud574\ubcf4\uc790.\r\nval lineList = mutableListOf&lt;String&gt;()\r\nFile(path).useLines { lines -&gt; lines.forEach { lineList.add(it) } }\r\nlineList.forEach { println(&quot;&gt; &quot; + it) }\r\n\r\n\/\/\uc774\ub807\uac8c \ucf54\ud2c0\ub9b0\uc758 \ud45c\uc900 \ud568\uc218\uc640 \ud568\uaed8 \ud30c\uc77c \uc77d\uae30\uc640 \uc4f0\uae30\ub97c \uad6c\ud604\ud558\uba74 \ucf54\ub4dc\ub97c \uac04\ub7b5\ud558\uac8c \uc791\uc131\ud560 \uc218 \uc788\ub2e4.\r\n\r\n\/\/\ud30c\uc77c \ubcf5\uc0ac\r\nFile(path).copyTo(File(&quot;\/Users\/seungcheonseo\/kproject\/data\/otr2.txt&quot;))\r\n\r\n\/\/\ud30c\uc77c\uc758 \ub0b4\uc6a9 \ucd9c\ub825\ud558\uae30\r\nFile(path).forEachLine { println(it) }\r\n\r\n\/\/\ubc14\uc774\ud2b8 \ub2e8\uc704\ub85c \uc77d\uae30 (\uc4f0\uae30\ub294 writeBytes())\r\nval bytes = File(path).readBytes()\r\nprintln(Arrays.toString(bytes))\r\n\r\n\/\/\uc904 \ub2e8\uc704\ub85c \uc77d\uae30\r\nval lines = File(path).readLines()\r\nlines.forEach { println(it) }\r\n\r\n\/\/\ud14d\uc2a4\ud2b8 \ub2e8\uc704\uc704\ub85c \uc77d\uae30 (\uc4f0\uae30\ub294 writeTest())\r\nval text = File(path).readText()\r\ntext.forEach { println(it) }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[349,7,4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/693"}],"collection":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=693"}],"version-history":[{"count":1,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/693\/revisions\/695"}],"wp:attachment":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}