{"id":178,"date":"2020-05-28T11:02:34","date_gmt":"2020-05-28T07:02:34","guid":{"rendered":"http:\/\/kidds.co.za\/?p=178"},"modified":"2020-05-28T11:02:34","modified_gmt":"2020-05-28T07:02:34","slug":"java-file-access-more-info","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2020\/05\/28\/java-file-access-more-info\/","title":{"rendered":"Java: File access more info"},"content":{"rendered":"\n<p>For those looking for a general strategy for reading and writing a string to file:<\/p>\n\n\n\n<p>credit to : https:\/\/stackoverflow.com\/users\/559634\/sharkalley<\/p>\n\n\n\n<p><strong>First, get a file object<\/strong><\/p>\n\n\n\n<p>You&#8217;ll need the storage path. For the internal storage, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File path = context.getFilesDir();<\/code><\/pre>\n\n\n\n<p>For the external storage (SD card), use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File path = context.getExternalFilesDir(null);<\/code><\/pre>\n\n\n\n<p>Then create your file object:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File file = new File(path, \"my-file-name.txt\");<\/code><\/pre>\n\n\n\n<p><strong>Write a string to the file<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileOutputStream stream = new FileOutputStream(file);\ntry {\n    stream.write(\"text-to-write\".getBytes());\n} finally {\n    stream.close();\n}<\/code><\/pre>\n\n\n\n<p>Or with Google Guava<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>String contents = Files.toString(file, StandardCharsets.UTF_8);<\/p><\/blockquote>\n\n\n\n<p><strong>Read the file to a string<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int length = (int) file.length();\n\nbyte&#91;] bytes = new byte&#91;length];\n\nFileInputStream in = new FileInputStream(file);\ntry {\n    in.read(bytes);\n} finally {\n    in.close();\n}\n\nString contents = new String(bytes);   <\/code><\/pre>\n\n\n\n<p>Or if you are using Google Guava<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String contents = Files.toString(file,\"UTF-8\");<\/code><\/pre>\n\n\n\n<p>For completeness I&#8217;ll mention<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String contents = new Scanner(file).useDelimiter(\"\\\\A\").next();<\/code><\/pre>\n\n\n\n<p>which requires no libraries, but benchmarks 50% &#8211; 400% slower than the other options (in various tests on my Nexus 5).<\/p>\n\n\n\n<p><strong>Notes<\/strong><\/p>\n\n\n\n<p>For each of these strategies, you&#8217;ll be asked to catch an IOException.<\/p>\n\n\n\n<p>The default character encoding on Android is UTF-8.<\/p>\n\n\n\n<p>If you are using external storage, you&#8217;ll need to add to your manifest either:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"\/><\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"\/><\/code><\/pre>\n\n\n\n<p>Write permission implies read permission, so you don&#8217;t need both.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those looking for a general strategy for reading and writing a string to file: credit to : https:\/\/stackoverflow.com\/users\/559634\/sharkalley First, get a file object You&#8217;ll need the storage path. For the internal storage, use: For the external storage (SD card), use: Then create your file object: Write a string to the file Or with Google [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":1,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/178\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}