I need to create a .txt file in a specific path that I created and write data that is in my BD inside that file, the way I found is OutputStreamWriter, but it sends me an error that says:
"java.lang.IllegalArgumentException: sample / example / example contains a path separator"
What I have researched is that the OutputStreamWriter method should not contain separators, in that case, how can I do it ?, since I necessarily need to put the file in the path that I specify and write the data, this is my code:
String CARPETA_PRINCIPAL = "MiCarpetaPrincipal/";
String CARPETA_DOCTXT = "MiCarpetaSecundaria";
String DIRECTORIO_TXT = CARPETA_PRINCIPAL + CARPETA_DOCTXT;
editText = (EditText)findViewById(R.id.txt_nombreArchivoExportar);
textView = (TextView)findViewById(R.id.view_rutaTxt);
public void exportarTxt(View view){
accessPermission();
String nombre_archivoPro = editText.getText().toString();
if (!nombre_archivoPro.isEmpty() || !nombre_archivoPro.equals("")){
File miFile = new File(Environment.getExternalStorageDirectory(), DIRECTORIO_TXT);
boolean isCreada = miFile.exists();
String nombreArchivo = "";
if (!isCreada){
isCreada = miFile.mkdirs();
} if (isCreada){
nombreArchivo = nombre_archivoPro + ".txt";
}
File folder = new File(Environment.getExternalStorageDirectory(), DIRECTORIO_TXT);
if (!folder.exists()){
folder.mkdir();
}
String nombre_completo = Environment.getExternalStorageDirectory() + File.separator + DIRECTORIO_TXT + File.separator + nombreArchivo;
File outputFile = new File(nombre_completo);
if (outputFile.exists()){
outputFile.delete();
}
try {
OutputStreamWriter archivo = new OutputStreamWriter(openFileOutput(nombre_completo, Activity.MODE_PRIVATE));
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "miBD", null, 1);
SQLiteDatabase BaseDeDatos = admin.getWritableDatabase();
Cursor fila = BaseDeDatos.rawQuery
("select codigo, cantidad from tablaDeMiBD", null);
while (fila.moveToNext()) {
archivo.write(fila.getString(0) + ", " + fila.getString(1) + "\n");
}
archivo.flush();
archivo.close();
} catch (Exception e){
Log.i("Error", String.valueOf(e));
}
Toast.makeText(this, "Archivo exportado Correctamente", Toast.LENGTH_LONG).show();
textView.setText(nombre_completo);
} else {
Toast.makeText(this, "Rellena el nombre del archivo que deseas antes de continuar", Toast.LENGTH_LONG).show();
}
}