Create text file: error java.lang.IllegalArgumentException: contains a path separator "

1

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();
        }
    }
    
asked by Humberto Arciniega 15.10.2018 в 18:39
source

1 answer

1

The error probably occurs in this line:

  OutputStreamWriter archivo = new OutputStreamWriter(openFileOutput(nombre_completo, Activity.MODE_PRIVATE));

The problem is that openFileOutput() does not accept separators, for this reason the error:

  

java.lang.IllegalArgumentException: example / example / example contains a   path separator

I suggest using FileOutputStream and OutputStreamWriter , this would be the change:

  try {
            //OutputStreamWriter archivo = new OutputStreamWriter(openFileOutput(nombre_completo, Activity.MODE_PRIVATE));

            /*------------------------------*/
            FileOutputStream fOut = new FileOutputStream(outputFile, true);
            OutputStreamWriter archivo = new OutputStreamWriter(fOut);
            /*------------------------------*/

            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));
     Log.i("Error", e.getMessage());
   }
    
answered by 15.10.2018 / 19:34
source