{"id":693,"date":"2019-12-30T23:07:46","date_gmt":"2019-12-30T14:07:46","guid":{"rendered":"http:\/\/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":"<p>[kt]<br \/>\n\/\/\ud30c\uc77c \uc4f0\uae30 1<br \/>\nimport java.io.IOException<br \/>\nimport java.nio.file.Files<br \/>\nimport java.nio.file.Paths<br \/>\nimport java.nio.file.StandardOpenOption<\/p>\n<p>fun main(){<br \/>\n    val path = &quot;\/file\/path\/hello.txt&quot;<br \/>\n    val text = &quot;\uc548\ub155\uc548\ub155\uc548\ub155\uc548\ub155n\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c&quot;<\/p>\n<p>    try {<br \/>\n        Files.write(Paths.get(path), text.toByteArray(), StandardOpenOption.CREATE)<br \/>\n    }catch(e: IOException){<br \/>\n\t\t\t\t\/\/Exception \ucc98\ub9ac<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/standardOpenOption\uc758 \uc635\uc158\uc740 CREATE, APPEND, WRITE, READ\uac00 \uc788\ub2e4.<br \/>\n[\/kt]<\/p>\n<p>[kt]<br \/>\n\/\/\ud30c\uc77c \uc4f0\uae30 2<br \/>\nimport java.io.File<br \/>\nimport java.io.FileWriter<br \/>\nimport java.io.PrintWriter<\/p>\n<p>fun main(){<br \/>\n    val outString: String = &quot;\uc548\ub155\ud558\uc2e0\uac00?tWhat are you doing now?nAre you Serious??rStrawberies!!&quot;<br \/>\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/testfile.txt&quot;<\/p>\n<p>    val file = File(path)<br \/>\n    val printWriter = PrintWriter(file)<\/p>\n<p>    printWriter.println(outString)<br \/>\n    printWriter.close()<\/p>\n<p>\t\t\/\/\uc774\uac83\ub3c4 \uac19\uace0<br \/>\n    File(path).printWriter().use{ out -&gt; out.println(outString) }<\/p>\n<p>\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.<br \/>\n    File(path).printWriter().use{ it.println(outString) }<br \/>\n}<\/p>\n<p>\/\/\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.<br \/>\n[\/kt]<\/p>\n<p>[kt]<br \/>\n\/\/\ud30c\uc77c \uc4f0\uae30 3<br \/>\nval file = File(path)<br \/>\nfile.writeText(outString)<br \/>\nfile.appendText(&quot;nDo Great Work!&quot;)<\/p>\n<p>val writer = FileWriter(path, true)<br \/>\ntry{<br \/>\n    writer.write(outString)<br \/>\n}catch(e: Exception){<br \/>\n    \/\/\uc624\ub958 \ubc1c\uc0dd!<br \/>\n}finally{<br \/>\n    writer.close()<br \/>\n}<br \/>\n[\/kt]<\/p>\n<p>[kt]<br \/>\n\/\/\ucf54\ud2c0\ub9b0 \ucc45 493\ud398\uc774\uc9c0 \ucc38\uc870<br \/>\n\/\/\ud30c\uc77c \uc77d\uae30 1<br \/>\nimport java.io.*<\/p>\n<p>fun main(){<br \/>\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/otr.txt&quot;<\/p>\n<p>    try{<br \/>\n        val read = FileReader(path)<br \/>\n        println(read.readText())<br \/>\n    }catch(e: Exception){<br \/>\n        println(e.message)<br \/>\n    }<br \/>\n}<br \/>\n[\/kt]<\/p>\n<p>[kt]<br \/>\n\/\/JAVA\uc2dd \ud30c\uc77c\uc77d\uae30 KOTLIN\uc73c\ub85c \ubcc0\uacbd<br \/>\nimport java.io.*<br \/>\nimport java.lang.Exception<\/p>\n<p>fun main(){<br \/>\n    val path = &quot;\/Users\/seungcheonseo\/kproject\/data\/otr.txt&quot;<\/p>\n<p>    \/\/\ub2e8\uc21c\ubcc0\ud658<br \/>\n    val file = File(path)<br \/>\n    val inputStream: InputStream = file.inputStream()<br \/>\n    val inputStreamReader = InputStreamReader(inputStream)<br \/>\n    val sb = StringBuilder()<br \/>\n    var line:  String?<br \/>\n    val br = BufferedReader(inputStreamReader)<br \/>\n    try{<br \/>\n        line = br.readLine()<br \/>\n        while(line != null){<br \/>\n            sb.append(line, &#8216;n&#8217;)<br \/>\n            line = br.readLine()<br \/>\n        }<br \/>\n        println(sb)<br \/>\n    }catch(e: Exception){<\/p>\n<p>    }finally{<br \/>\n        br.close()<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>\/\/\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.<\/p>\n<p>\/\/\uc644\uc804 \ucf54\ud2c0\ub9b0\uc2dd\uc73c\ub85c \ubcc0\ud658!<br \/>\nval file = File(path)<br \/>\nval inputStream: InputStream = file.inputStream()<br \/>\nval text = inputStream.bufferedReader().use { it.readText() }<br \/>\nprintln(text)<\/p>\n<p>\/\/Java\uc758 InputStream\uc5d0\ub294 bufferedReader\uac00 \uc5c6\uc9c0\ub9cc,<br \/>\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.<\/p>\n<p>\/\/file\uac1d\uccb4\ub97c \uc0dd\ub7b5\ud558\uace0 \uc774\ub807\uac8c\ub3c4 \uc218\uc815 \uac00\ub2a5<br \/>\nval bufferedReader: BufferedReader = File(path).bufferedReader()<br \/>\nval inputString = bufferedReader.use { it.readText() }<br \/>\nprintln(inputString)<\/p>\n<p>\/\/\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.<br \/>\nval bufferedReader = File(path).bufferedReader()<br \/>\nval lineList = mutableListOf&lt;String&gt;()<br \/>\nbufferedReader.useLines { lines -&gt; lines.forEach { lineList.add(it) } }<br \/>\nlineList.forEach { println(&quot;&gt; &quot; + it) }<\/p>\n<p>\/\/\uc774\uc81c bufferedReader\uae4c\uc9c0 \uc0dd\ub7b5\ud558\uace0 \ud30c\uc77c\uc744 \uc9c1\uc811 \uc0ac\uc6a9\ud574\ubcf4\uc790.<br \/>\nval lineList = mutableListOf&lt;String&gt;()<br \/>\nFile(path).useLines { lines -&gt; lines.forEach { lineList.add(it) } }<br \/>\nlineList.forEach { println(&quot;&gt; &quot; + it) }<\/p>\n<p>\/\/\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.<\/p>\n<p>\/\/\ud30c\uc77c \ubcf5\uc0ac<br \/>\nFile(path).copyTo(File(&quot;\/Users\/seungcheonseo\/kproject\/data\/otr2.txt&quot;))<\/p>\n<p>\/\/\ud30c\uc77c\uc758 \ub0b4\uc6a9 \ucd9c\ub825\ud558\uae30<br \/>\nFile(path).forEachLine { println(it) }<\/p>\n<p>\/\/\ubc14\uc774\ud2b8 \ub2e8\uc704\ub85c \uc77d\uae30 (\uc4f0\uae30\ub294 writeBytes())<br \/>\nval bytes = File(path).readBytes()<br \/>\nprintln(Arrays.toString(bytes))<\/p>\n<p>\/\/\uc904 \ub2e8\uc704\ub85c \uc77d\uae30<br \/>\nval lines = File(path).readLines()<br \/>\nlines.forEach { println(it) }<\/p>\n<p>\/\/\ud14d\uc2a4\ud2b8 \ub2e8\uc704\uc704\ub85c \uc77d\uae30 (\uc4f0\uae30\ub294 writeTest())<br \/>\nval text = File(path).readText()<br \/>\ntext.forEach { println(it) }<br \/>\n[\/kt]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[kt] \/\/\ud30c\uc77c \uc4f0\uae30 1 import java.io.IOException import java.nio.file.Files import java.nio.file.Paths import java.nio.file.StandardOpenOption fun main(){ val path = &quot;\/file\/path\/hello.txt&quot; val text = &quot;\uc548\ub155\uc548\ub155\uc548\ub155\uc548\ub155n\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c\ud5ec\ub85c&quot; try { Files.write(Paths.get(path), text.toByteArray(), StandardOpenOption.CREATE) }catch(e: IOException){ \/\/Exception \ucc98\ub9ac } } \/\/standardOpenOption\uc758 \uc635\uc158\uc740 CREATE, APPEND, WRITE, READ\uac00 \uc788\ub2e4. [\/kt] [kt] \/\/\ud30c\uc77c \uc4f0\uae30 2 import java.io.File import java.io.FileWriter import java.io.PrintWriter fun main(){ val outString: &hellip; <\/p>\n","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":[],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-development","category-it"],"_links":{"self":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"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}]}}