I am trying to write a text file to the external storage, I am wondering how to append to the filename so that I can store unique files and not overwrite the past ones.
As well, I see the file in the file explorer on android studio, but can't find it on my phone, the same thing happens when I use an emulator, I don't see it in the file explorer but can verify it exists when using the device explorer.
My code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button btnSave, btnLoad;
EditText etInput;
TextView tvLoad;
String fileName = "";
String filePath = "";
String fileContent = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave = findViewById(R.id.buttonSave);
btnLoad = findViewById(R.id.buttonLoad);
etInput = findViewById(R.id.etInput);
tvLoad = findViewById(R.id.tvLoad);
fileName = "myFile.txt";
filePath = "myFileDir";
if(!isExternalStorageAvailableForRW()){
btnSave.setEnabled(false);
}
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvLoad.setText("");
fileContent = etInput.getText().toString().trim();
if(!fileContent.equals("")){
File myExternalFile = new File(getExternalFilesDir(filePath), fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myExternalFile);
fos.write(fileContent.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
etInput.setText("");
Toast.makeText(MainActivity.this,"File Saved, check dir", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Textfield empty", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isExternalStorageAvailableForRW() {
String extStorageState = Environment.getExternalStorageState();
if(extStorageState.equals(Environment.MEDIA_MOUNTED)){
return true;
}
return false;
}
}
For
Java 7+
For a one-time task, the Files class makes this easy:
try {
Files.write(Paths.get("filePath"), "the text".getBytes(), StandardOpenOption.APPEND);
}catch (IOException e) {
//exception handling left as an exercise for the reader
}
The above approach will throw a
NoSuchFileException
if the file does not already exist. It also does not append a newline automatically (which you often want when appending to a text file). Another approach is to pass both CREATE and APPEND options, which will create the file first if it doesn't already exist:
private void write(final String s) throws IOException {
Files.writeString(
Path.of(System.getProperty("java.io.tmpdir"), "filePath"),
s + System.lineSeparator(),
CREATE, APPEND
);
}