Write Text File to Internal Storage – Android Studio Tutorial
In this video we will save a string to the internal storage of our device. We will use a FileOutputStream to save the data in bytes. Then we use a FileInputStream and a BufferedReader to read them back into our app and rebuild our string using a StringBuilder. We will also catch any IOExceptions.
Source code:
____________________
❗ Subscribe to the channel:
🎓 Find more tutorials and courses on my website:
💻 Hire me for your next project:
📨 Subscribe to the Coding in Flow newsletter:
💚 Donate to Coding in Flow with a message:
❓ Ask your programming questions in our chat:
📣 Follow Coding in Flow on other sites:
Facebook:
Instagram:
Twitter:
Github:
⌨️ My gear and tools:
Microphone:
Mouse:
Keyboard:
Desk:
Monitor:
Webcam:
Screen recorder:
(above links are affiliate links, which means that I earn a small commission if you buy through them with NO additional costs to you.)
what about saving data other than texts (for eg., spinner item, textview, layout children, etc) ??
How can we these kind of data.
I have watched several of your tutorials. You are doing an absolutely SUPERB job!!! Very clear and to the point. Thank you so much!
3:33 when you use openFileOutput() I get an error, that method doesn't seem to exist…
Noo waaaay, coding in flow is using dark theme 😯😯
Hello, can you make a tutorial on
How to create an App (folder/directory) for android 11.
I cant store "n"(enter) in it
It comes like in one line
Are Android rights needed? If yes must the user accept it?
Sir, if we want to store audio file in list view. What are the steps?
How can i use to append because data is updated every time. I don't want this I want it to continue from the last String. Or what can I use instead of Append for this, thanks for everything.
What to do in case of pdf file?
Bro I have a doubt pls do explain me…. I did all the things u told but atlast I can't see my folder or file in my phone's storage…!may I know why..?
Really helpful
Thank You!! This video helped me a lot!
This tutorial is so good, that even though it is not the answer to my problem, I want to follow it, just to get this code under my fingers.
9:36 bubuvuuend
thanks for explaining codes man really helpful
I Create A PDF File On My App, But Android 10 Don't Save This pdf File, But Android 5.1 Work fine .
Sir Please Help me Please Please…………
Hello! I have a question to you. Not directly related with the code but with the recording. What tool do you use to make zoom of the code when you are recording?
Thank you.
How do you save a document? I have a worksheet in png for people to draw on but would like them to save that document….
Save app data to Google drive as a backup
How to do that.. ? Give a like so I can get a notification when you answer.. and my app is available on playstore too.. which I made from combining four tutorials of you. 🙏
What i have to do if I want to append text instead of overwriting it?
Love this video.Helped me a ton in creating a text log file .Thank You.
How can I access those files in my PC, it says that there's any file on the folder, but my app can read it… Can you help me?
Added one more new feature thanks to you
how do I set the address in the phone when I save file
that dialect sounds like you are from Germany, @Coding in Flow. Grüße vom Bodensee 🙂
Nice man!
Hello, my friend!
I love your tutorials because they always work!
I have a small question:
– How can I save it to another directory? In this example, it is saved in the folders from which the app is mounted on the system. For example, I wanted to save to the /Downloads directory.
How could this be made?
Thank you very much!
I LOVE YOU <3
In с# we can read and write files in one commad😥
how to do it in Andriod Q
I'm trying to make us of this method to simply append lines of text to a file. I have put these methods in a seperate class but when I call the append method with the click of a button in an activity I get a crash and can't seem to get to the bottom of it.
I Have the constructor which takes in the name of the file to be written to when a "Violation logger" object (in my case that is what this is called) and it returns the desired text file name as defined in strings.text and fed into said constructor.
Seems like FOS is always null? Pls help>?
This is my Violation_Logger Class.
——————————————————————————————————————————————————————————————————————————————————————————————————————
import android.app.Application;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Violation_Logger extends Application {
static String LOG_LOCATION = "LoggingError";
public Violation_Logger(String logFileName)
{
LOG_LOCATION = logFileName;
Log.d("MyDebug", "Violation Logger target is " + logFileName);
}
public void AppendViolation(String violation)
{
FileOutputStream fos = null;
try {
fos = openFileOutput(LOG_LOCATION, MODE_PRIVATE);
fos.write(violation.getBytes());
String saveLogMessage = String.format("Saving Violation: " + violation + " to " + getFilesDir() + "/" + LOG_LOCATION);
Log.d("MyDebug", saveLogMessage);
} catch (FileNotFoundException e) {
// Create the File!
// Write again?
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();//We couldnt do this for some other reason.
}finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String load()
{
FileInputStream fis = null;
String violationsLog = "LOG LOADING ERROR";
try {
fis = openFileInput(this.LOG_LOCATION);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((violationsLog = br.readLine()) != null){
sb.append(violationsLog).append("n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return violationsLog;
}
}
Thanks, Thanks, Thanks <3
My app tries to save data to /data/user/0/…. , but since it is not rooted, it can not. How can I solve that problem?
the only problem is how i can open that file from explorer ? or can i copy the data from that file ? actually i am loading SMS on this file but i cannot do any thing to it becuase its protected what would be the solution ? my ideas are , either output on SD card ( but not all phones have SD cards ) then how to save it to a user friendly location to be accessed later from locked phones explorers ( i can access this file through root but not other wise ) please help