OpenFileOutput () Method and FileOutputStream () Constructor

Why should you use a openFileOutput()

method instead of a constructor in Android FileOutputStream()

?

Will the mode type as the second parameter to openFileOutput () be the only "valid" reason for all cases?

FileOutputStream fos;
fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
fos = new FileOutputStream("test.txt");

      

+3


source to share


2 answers


Will the mode type as the second parameter to openFileOutput () be the only "valid" reason for all cases?

Another difference is that it openFileOutputStream

opens / creates a file in the "internal" storage of the device. On the contrary, FileOutputStream

it allows for both internal and external storage.

The third difference is that it openFileOutputStream

writes files in the context of the current application, while it FileOutputStream

can write in any context ... modulo possible permissions.



(Both versions can open files in append mode. This is not the point of difference.)

Link:

0


source


openFileOutput

specially used to write files to internal memory and prohibit writing to external storage. However, FileOutputStream

it allows you to record both internal and external storage. In my experience, you can easily create a directory using FileOutputStream in internal storage. You can also set the mode using FileOutputStream as the second parameter in one of the constructors . An example of how you write to internal memory using FileOutputStream in append mode:



 File filedir = new File(MyApplication.getAppContext().getDir("DirectoryNameYouWant"));
File filename = new File("FilenameYouWant");
FileOutPutStream fOut=new FileOutputStream(new File(filedir,filename),true);

      

+2


source







All Articles