Basically i have two questions. i am using the below code to read and write z text file.
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append("my text here");
myOutWriter.close();
this create a new file every time i want this to OPEN_OR_CREATE(if file already exist don’t create a new one)
Ad my second question is that how to change the path “/sdcard/mysdfile.txt” i want this file to stored in my sdcard -> subFolder1 -> SubFolder2
Thnaks
Do not use hardcoded
/sdcardor/mnt/sdcardor your app will fail as devices vary on location or mountpoint of that storage. To get the right location useSee docs here.
To append content to existing file use
new FileOutputStream(myFile, true);instead of justnew FileOutputStream(myFile);– see docs on that constructor here.As for
Aside from getting rid of
/sdcardas said above, just add subfolders to the paths:MyFolder1/MyFolder2/mysdfile.txt. Note these folder have to exists or the path will be invalid. You can always create it by callingmyFile.mkdirs().