{"id":2521,"date":"2017-05-05T10:54:43","date_gmt":"2017-05-05T10:54:43","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2521"},"modified":"2017-05-05T19:07:06","modified_gmt":"2017-05-05T19:07:06","slug":"how-to-create-and-write-data-into-the-file-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-create-and-write-data-into-the-file-in-android\/","title":{"rendered":"How to Create and Write Data into the File in Android"},"content":{"rendered":"<p>In this tutorial how to create and write data into the file in an Android Application is shown.<\/p>\n<p>We can read and write data in an internal memory as well as external memory of our mobile.<\/p>\n<p>When we read or write data in an external memory then we have to set permission in AndroidManifest.xml file, but when you read or write the data in internal memory then no permissions are required.<\/p>\n<p>The permission to set for external storage to write the data in external file is as below:<\/p>\n<p>&lt;manifest&gt;<\/p>\n<p>&lt;uses-permission android:name=&#8221;android:permission.WRITE_EXTERNAL_STORAGE&#8221;\/&gt;<\/p>\n<p>&lt;\/manifest&gt;<\/p>\n<p>Write the above code after the &lt;uses-sdk&gt; tag and before the &lt;application&gt; tag.<\/p>\n<p>To acquire a proper directory we can call below methods<\/p>\n<ol>\n<li>getFilesDir()<\/li>\n<li>detCacheDir()<\/li>\n<\/ol>\n<p>To create new File, create the object of File class like below:<\/p>\n<p>File file=new File(context.getFilesDir(),filename);<\/p>\n<p>We can call openFileOutput() method to get FileOutputStream.<\/p>\n<p>When we open our file in a MODE_PRIVATE then it will overwrite the data, if file exists, if it does not exist then it will create a new file.<\/p>\n<p>When we open our file in a MODE_APPEND then it will\u00a0append the new data from the last line or end of the file, if file exists, if it does not exist then it will create a new file.<\/p>\n<p>Point to Remember:<\/p>\n<ul>\n<li>If you want to see where your Application&#8217;s File are stored follow the below mentioned steps:\n<ol>\n<li>Click the DDMS button on the top<\/li>\n<li>Open the File Explorer<\/li>\n<li>Open Data folder<\/li>\n<li>Again open the Data folder<\/li>\n<li>Open your package name folder, for ex. com.example.demo<\/li>\n<li>Open &#8216;files&#8217; folder<\/li>\n<li>Your File data are stored in the file present inside the \u00a0&#8216;files&#8217; folder<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>This is the code for file internal storage.<\/p>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;TextView<br \/>\nandroid:id=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;69dp&#8221;<br \/>\nandroid:text=&#8221;File I\/O Operations&#8221; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&#8221;@+id\/create1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_below=&#8221;@+id\/textView1&#8243;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;36dp&#8221;<br \/>\nandroid:onClick=&#8221;onFileCreate&#8221;<br \/>\nandroid:text=&#8221;Create File&#8221; \/&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.content.Intent;<br \/>\nimport android.view.Menu;<br \/>\nimport android.view.View;<\/p>\n<p>public class MainActivity extends Activity{<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\n}<\/p>\n<p>public void onFileCreate(View v)<br \/>\n{<br \/>\nIntent in=new Intent(MainActivity.this,create.class);<br \/>\nstartActivity(in);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>res\/layout\/create_layout.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221; &gt;<\/p>\n<p>&lt;EditText<br \/>\nandroid:id=&#8221;@+id\/fnm1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;85dp&#8221;<br \/>\nandroid:ems=&#8221;10&#8243;<br \/>\nandroid:hint=&#8221;Enter File Name&#8221; &gt;<\/p>\n<p>&lt;requestFocus \/&gt;<br \/>\n&lt;\/EditText&gt;<\/p>\n<p>&lt;EditText<br \/>\nandroid:id=&#8221;@+id\/fdt1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_below=&#8221;@+id\/fnm1&#8243;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;41dp&#8221;<br \/>\nandroid:ems=&#8221;10&#8243;<br \/>\nandroid:hint=&#8221;Enter the data&#8221; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&#8221;@+id\/create2&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignLeft=&#8221;@+id\/fdt1&#8243;<br \/>\nandroid:layout_below=&#8221;@+id\/fdt1&#8243;<br \/>\nandroid:layout_marginTop=&#8221;78dp&#8221;<br \/>\nandroid:onClick=&#8221;createf&#8221;<br \/>\nandroid:text=&#8221;Create File&#8221; \/&gt;<\/p>\n<p>&lt;Button<br \/>\nandroid:id=&#8221;@+id\/back1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignBottom=&#8221;@+id\/create2&#8243;<br \/>\nandroid:layout_alignRight=&#8221;@+id\/fdt1&#8243;<br \/>\nandroid:onClick=&#8221;back&#8221;<br \/>\nandroid:text=&#8221;Back&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>src\/com.package.demo\/create.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import java.io.FileOutputStream;<br \/>\nimport java.io.IOException;<\/p>\n<p>import android.app.Activity;<br \/>\nimport android.content.Intent;<br \/>\nimport android.os.Bundle;<br \/>\nimport android.view.View;<br \/>\nimport android.widget.EditText;<br \/>\nimport android.widget.Toast;<\/p>\n<p>public class create extends Activity{<br \/>\nFileOutputStream fOut;<br \/>\nEditText fnm,fdt;<br \/>\nString data,fname;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.create_layout);<br \/>\nfnm=(EditText)findViewById(R.id.fnm1);<br \/>\nfdt=(EditText)findViewById(R.id.fdt1);<br \/>\n}<\/p>\n<p>public void createf(View v) throws IOException<br \/>\n{<br \/>\nfname=fnm.getText().toString();<br \/>\ndata=fdt.getText().toString();<br \/>\ntry<br \/>\n{<br \/>\nfOut=openFileOutput(fname,MODE_APPEND);<br \/>\nfOut.write(data.getBytes());<br \/>\nfOut.close();<br \/>\nToast.makeText(getBaseContext(), &#8220;File is successfully created and data is successfully saved in file&#8221;, Toast.LENGTH_SHORT).show();<br \/>\n}<br \/>\ncatch(Exception e){<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\nfinally<br \/>\n{<br \/>\nfOut.close();<br \/>\n}<br \/>\n}<\/p>\n<p>public void back(View v)<br \/>\n{<br \/>\nIntent in=new Intent(this,MainActivity.class);<br \/>\nstartActivity(in);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>AndroidManifest.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;manifest xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\npackage=&#8221;com.example.demo&#8221;<br \/>\nandroid:versionCode=&#8221;1&#8243;<br \/>\nandroid:versionName=&#8221;1.0&#8243; &gt;<\/p>\n<p>&lt;uses-sdk<br \/>\nandroid:minSdkVersion=&#8221;8&#8243;<br \/>\nandroid:targetSdkVersion=&#8221;17&#8243; \/&gt;<\/p>\n<p>&lt;application<br \/>\nandroid:allowBackup=&#8221;true&#8221;<br \/>\nandroid:icon=&#8221;@drawable\/ic_launcher&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221;<br \/>\nandroid:theme=&#8221;@style\/AppTheme&#8221; &gt;<br \/>\n&lt;activity<br \/>\nandroid:name=&#8221;com.example.demo.MainActivity&#8221;<br \/>\nandroid:label=&#8221;@string\/app_name&#8221; &gt;<br \/>\n&lt;intent-filter&gt;<br \/>\n&lt;action android:name=&#8221;android.intent.action.MAIN&#8221; \/&gt;<\/p>\n<p>&lt;category android:name=&#8221;android.intent.category.LAUNCHER&#8221; \/&gt;<br \/>\n&lt;\/intent-filter&gt;<br \/>\n&lt;\/activity&gt;<br \/>\n&lt;activity android:name=&#8221;.create&#8221;&gt;&lt;\/activity&gt;<\/p>\n<p>&lt;\/application&gt;<\/p>\n<p>&lt;\/manifest&gt;<\/p>\n<p><a href=\"https:\/\/youtu.be\/MCqwnfFFHRk\">https:\/\/youtu.be\/MCqwnfFFHRk<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to create and write data into the file in an Android Application is shown. We can read and write data in an internal memory as well as external memory of our mobile. When we read or write data in an external memory then we have to set permission in AndroidManifest.xml file, but when you read or write the data in internal memory then no permissions are required. The permission to set for external storage to write the data in external file is as below: &lt;manifest&gt; &lt;uses-permission android:name=&#8221;android:permission.WRITE_EXTERNAL_STORAGE&#8221;\/&gt; &lt;\/manifest&gt; Write the above code after the &lt;uses-sdk&gt; tag and before the &lt;application&gt; tag. To acquire a proper directory we can call below methods getFilesDir() detCacheDir() To create new <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[109,108,110],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2521"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2521"}],"version-history":[{"count":5,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2521\/revisions"}],"predecessor-version":[{"id":2532,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2521\/revisions\/2532"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}