How to write data with FileOutputStream without losing old data?
Use the constructor that takes a File
and a boolean
FileOutputStream(File file, boolean append)
and set the boolean to true
. That way, the data you write will be appended to the end of the file, rather than overwriting what was already there.
Write to file using ObjectOutputStream without overwriting old data
You should open file like this for appending string in the file
new FileOutputStream(file, true)
Creates a file output stream to write to the file represented by the
specified File object. If the second argument is true, then bytes will
be written to the end of the file rather than the beginning. A new
FileDescriptor object is created to represent this file connection.
But Java serialization does not support "appending". you can't write an ObjectOutputStream
to a file, then open the file again in append mode and write another ObjectOutputStream
to it. you have to re-write the entire file every time. (i.e. if you want to add objects to the file, you need to read all the existing objects, then write the file again with all the old objects and then the new objects).
I would sugest you to use DataOutputStream
public void writeSelling(List<String> wordList) throws IOException {
fileOutPutStream = new FileOutputStream (file,true);
DataOutputStream write =new DataOutputStream(fileOutPutStream);
for (String s : wordList){
d.writeUTF(s);
}
write.close();
}
java write data to file without erasing the old content
Use new FileOutputStream(file, true)
. This will open file in "append" mode which will append all data written to the stream to the end of that file.
FileOutputStream Deletes Contents of File
The JavaDoc says the following
/**
* Creates a file output stream to write to the file represented by
* the specified <code>File</code> object. If the second argument is
* <code>true</code>, then bytes will be written to the end of the file
* rather than the beginning. A new <code>FileDescriptor</code> object is
* created to represent this file connection.
* <p>
* First, if there is a security manager, its <code>checkWrite</code>
* method is called with the path represented by the <code>file</code>
* argument as its argument.
* <p>
* If the file exists but is a directory rather than a regular file, does
* not exist but cannot be created, or cannot be opened for any other
* reason then a <code>FileNotFoundException</code> is thrown.
*
* @param file the file to be opened for writing.
* @param append if <code>true</code>, then bytes will be written
* to the end of the file rather than the beginning
* @exception FileNotFoundException if the file exists but is a directory
* rather than a regular file, does not exist but cannot
* be created, or cannot be opened for any other reason
* @exception SecurityException if a security manager exists and its
* <code>checkWrite</code> method denies write access
* to the file.
* @see java.io.File#getPath()
* @see java.lang.SecurityException
* @see java.lang.SecurityManager#checkWrite(java.lang.String)
* @since 1.4
*/
public FileOutputStream(File file, boolean append)
Thus to use it you should add a boolean to the constructor of your FileOutputStream, True to append or False to overwrite
So your code should look like this:
Boolean append = true;
outS = new FileOutputStream(videoFile, append);
When writing a huge amount of data, parts of it get lost / When every data is present, the write process is very slow
Maybe this can help you
Fastest way to write huge data in text file Java
https://www.quora.com/How-do-to-read-and-write-large-size-file-in-Java-efficiently
FileOutputStream formatting?
Why not just construct the String(s) first and then call
getBytes()
on the resulting String and pass it to fos.write()
?
Example:
FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE);
String bigstring = "Balls: " + balls + " Strikes: " + strikes;
fos.write(bigstring.getBytes());
fos.close();
If you want to insert a newline into the String you can do it by concatenating a new-line character into it.
FileOutput Stream delets text file contents
You should try with this constructor :
FileOutputStream fos = new FileOutputStream(f, true);
So what you have to add to the file will be appended if it already exists.
Doc available here
Related Topics
Https Get (Ssl) with Android and Self-Signed Server Certificate
Setting Custom Actionbar Title from Fragment
Failed to Run Sdkmanager --List with Java 9
Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This
Unexpected Top-Level Exception: Com.Android.Dex.Dexexception: Multiple Dex Files Define
Volley - Sending a Post Request Using JSONarrayrequest
Retrofit2 Android: Expected Begin_Array But Was Begin_Object at Line 1 Column 2 Path $
Android.Util.Androidruntimeexception: Requestfeature() Must Be Called Before Adding Content
Change Background Color of Single Specific Menu Items of Navigationview
Listview Is Blank While Using Getfilter Function
How to Generate Random Number in Specific Range in Android
How to View My Realm File in the Realm Browser
How to Create a Bks (Bouncycastle) Format Java Keystore That Contains a Client Certificate Chain
How to Set Android Lock Screen Image
Android M Permissions: Onrequestpermissionsresult() Not Being Called