Course Content

Course Content

Internal Storage Example

Internal Storage Example in Android

We have seen that how we can store data in external storage, now we are going to see that how we can save a file to internal storage in Android with Example.

Internal Storage Example Stepwise

Step-1 In internal storage we we will not need to specify any particular permission to save the file in our 'AndroidManifest.xml' for internal storage as we did in the case of external storage.

Step-2 For layout in our 'activity_main.xml' file we have taken two editText, two textView, and two Buttons. We have provided all of them with basic properties and have given them specified IDs.

Step-3 Now we will open our 'MainActivity.java' file which is inside 'java' folder, and see how internal storage's file is different from external storage. All the contents which we have taken in our 'activity_main.xml' file we have redefined them all in here, like we have define editText with instance, buttons, and we have done their view identification by using 'FindViewByID'.

Step-4 Now we will see that how we are using file in internal storage, we will create an instance of 'FileOutputStream' because we want to write content first,

FileOutputStream fos;

try

{

fos=openFileOutput(filename,context,MODE_PRIVATE)

}

External storage is public mode while internal storage is a private mode, therefore we are using ‘MODE_PRIVATE’. Now we need to write our content so,

fos.write(data.getBytes());

fos.close();

 

Step-5 After that we will use toast notification to make a text appear called 'saved' as soon as our file gets saved,

Toast.makeText(getApplicationContext(),filename + ""saved"",Toast,LENGTH_LONG,show();

 

  1. Now we will see that how we are coding our second button 'Read',

readbutton.setOnClickListener(new OnClickListener ()

{

@Override

public void onClick (View arg())

{

String filename=editTextFilename.getText().toString[];

StringBuffer stringbuffer=new BufferReader(new InputStreamReader (openFileInput(filename));

 

Step-6 When we compare 'read' button with external storage we notice that we have passed instance of 'fileinput' in 'InputStreamReader', while in internal storage the instance of BufferReader which we created, we will use 'OpenFileInput' to pass our filename.

Step-7 Now after that using 'while' loop we have given condition through input string for read button,

while { (InputString = myReader.readLine([] !=null)

{

stringBuffer.append(inputString + ""\\n"");

}

Step-8 Then after that we have to use toast notification,

Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();

Step-9 Now after that we will execute our code, when our code runs first of all we have to provide a file name, and then the data, and than click on Save. Our code is running successfully.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere