{"id":173,"date":"2020-05-28T10:43:14","date_gmt":"2020-05-28T06:43:14","guid":{"rendered":"http:\/\/kidds.co.za\/?p=173"},"modified":"2020-05-28T12:39:42","modified_gmt":"2020-05-28T08:39:42","slug":"java-read-and-write-from-file","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2020\/05\/28\/java-read-and-write-from-file\/","title":{"rendered":"Java: Read and Write from file"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import android.view.View;\nimport android.widget.EditText;\nimport android.widget.Toast;\nimport java.io.BufferedReader;\nimport java.io.FileInputStream;\nimport java.io.FileNotFoundException;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\npublic class MainActivity extends AppCompatActivity {\n    private static final String FILE_NAME = \"example.txt\";\n    EditText mEditText;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        mEditText = findViewById(R.id.edit_text);\n    }\n    public void save(View v) {\n        String text = mEditText.getText().toString();\n        FileOutputStream fos = null;\n        try {\n            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);\n            fos.write(text.getBytes());\n            mEditText.getText().clear();\n            Toast.makeText(this, \"Saved to \" + getFilesDir() + \"\/\" + FILE_NAME,\n                    Toast.LENGTH_LONG).show();\n        } catch (FileNotFoundException e) {\n            e.printStackTrace();\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            if (fos != null) {\n                try {\n                    fos.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n    public void load(View v) {\n        FileInputStream fis = null;\n        try {\n            fis = openFileInput(FILE_NAME);\n            InputStreamReader isr = new InputStreamReader(fis);\n            BufferedReader br = new BufferedReader(isr);\n            StringBuilder sb = new StringBuilder();\n            String text;\n            while ((text = br.readLine()) != null) {\n                sb.append(text).append(\"\\n\");\n            }\n            mEditText.setText(sb.toString());\n        } catch (FileNotFoundException e) {\n            e.printStackTrace();\n        } catch (IOException e) {\n            e.printStackTrace();\n        } finally {\n            if (fis != null) {\n                try {\n                    fis.close();\n                } catch (IOException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,13],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-java","category-snippets-java"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/173","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=173"}],"version-history":[{"count":8,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}