How To Download A Text File In Block Notes From Android Phone UPDATED
How To Download A Text File In Block Notes From Android Phone
Today we will look into android internal storage. Android offers a few structured ways to shop data. These include
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Storage
- Storage via Network Connection(on cloud)
In this tutorial we are going to look into the saving and reading data into files using Android Internal Storage.
Android Internal Storage
Android Internal storage is the storage of the individual data on the device retentivity. Past default, saving and loading files to the internal storage are private to the awarding and other applications will not have access to these files. When the user uninstalls the applications the internal stored files associated with the application are also removed. However, notation that some users root their Android phones, gaining superuser access. These users volition be able to read and write whatever files they wish.
Reading and Writing Text File in Android Internal Storage
Android offers openFileInput
and openFileOutput
from the Java I/O classes to alter reading and writing streams from and to local files.
- openFileOutput(): This method is used to create and salvage a file. Its syntax is given beneath:
FileOutputStream fOut = openFileOutput("file name",Context.MODE_PRIVATE);
The method
openFileOutput()
returns an example ofFileOutputStream
. After that nosotros can call write method to write data on the file. Its syntax is given below:String str = "test data"; fOut.write(str.getBytes()); fOut.close();
- openFileInput(): This method is used to open a file and read it. It returns an instance of FileInputStream. Its syntax is given below:
FileInputStream fin = openFileInput(file);
Afterward that, we call read method to read one character at a time from the file and and so print it. Its syntax is given below:
int c; Cord temp=""; while( (c = fin.read()) != -1){ temp = temp + Character.toString((char)c); } fin.shut();
In the in a higher place lawmaking, string temp contains all the data of the file.
- Note that these methods practise non have file paths (due east.g. path/to/file.txt), they just take elementary file names.
Android Internal Storage Project Structure
Android Internal Storage Example Code
The xml layout contains an EditText
to write information to the file and a Write Button and Read Button. Annotation that the onClick
methods are defined in the xml file simply as shown beneath:
activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="truthful" android:layout_alignParentRight="true" android:padding="5dp" android:text="Android Read and Write Text from/to a File" android:textStyle="bold" android:textSize="28sp" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="22dp" android:minLines="five" android:layout_margin="5dp"> <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Write Text into File" android:onClick="WriteBtn" android:layout_alignTop="@+id/button2" android:layout_alignRight="@+id/editText1" android:layout_alignEnd="@+id/editText1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Read Text From file" android:onClick="ReadBtn" android:layout_centerVertical="truthful" android:layout_alignLeft="@+id/editText1" android:layout_alignStart="@+id/editText1" /> </RelativeLayout>
The MainActivity
contains the implementation of the reading and writing to files as it was explained in a higher place.
package com.journaldev.internalstorage; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class MainActivity extends Activeness { EditText textmsg; static final int READ_BLOCK_SIZE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textmsg=(EditText)findViewById(R.id.editText1); } // write text to file public void WriteBtn(View v) { // add-write text into file try { FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE); OutputStreamWriter outputWriter=new OutputStreamWriter(fileout); outputWriter.write(textmsg.getText().toString()); outputWriter.close(); //display file saved message Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).bear witness(); } catch (Exception eastward) { eastward.printStackTrace(); } } // Read text from file public void ReadBtn(View v) { //reading text from file effort { FileInputStream fileIn=openFileInput("mytextfile.txt"); InputStreamReader InputRead= new InputStreamReader(fileIn); char[] inputBuffer= new char[READ_BLOCK_SIZE]; String due south=""; int charRead; while ((charRead=InputRead.read(inputBuffer))>0) { // char to cord conversion String readstring=String.copyValueOf(inputBuffer,0,charRead); s +=readstring; } InputRead.close(); textmsg.setText(s); } catch (Exception due east) { e.printStackTrace(); } } }
Hither, a toast is displayed when information is successfully written into the internal storage and the information is displayed in the EditText itself on reading the information from the file.
The epitome shown below is the output of the project. The image depicts text being written to the internal storage and on clicking Read it displays back the text in the same EditText.
Where is the file located?
To actually view the file open the Android Device Monitor from Tools->Android->Android Device Monitor.
The file is nowadays in the folder data->information->{bundle proper noun}->files as shown in the images below:
The file "mytextfile.txt" is institute in the package name of the project i.e. com.journaldev.internalstorage as shown below:
Download the final project for android internal storage case from below link.
DOWNLOAD HERE
Posted by: duffywhailee.blogspot.com
0 Response to "How To Download A Text File In Block Notes From Android Phone UPDATED"
Post a Comment