Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.6K
Vistas
Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory)

I found lot of topics with the same problem, but they couldn't fix mine. I initially write a file as follow:`

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
    if (!root.exists()) {
        root.mkdirs();
    }
    File notefile = new File(root, sFileName);
    FileWriter writer = null;
    try {
        writer = new FileWriter(notefile);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.append(sBody);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.flush();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Don't worry about the try and catch blocks, i will clear them later :D. And this is the reader which should works in the same directory ("Notes" of the sdcard, if it doesn't exist, will be created), read the file, and put it on a Notify as you can see:`

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
    if (!root.exists()) {
        root.mkdirs();
    }

    File file = new File(root, "Nota.txt");


    //Read text from file
    text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
        br.close() ;
    }catch (IOException e) {
        e.printStackTrace();
    }

I really don't understand why i get this problem, i even try with

getExternalStorageDirectory().getAbsolutePath() 

but without success.

Can someone help me?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You've tried to check with a debugger if root exists when you do:

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
    root.mkdirs();
}

I do not think it will create the folder when you write the file

over 4 years ago · Santiago Trujillo Denunciar

0

To write a file in external storage, you need to have WRITE_EXTERNAL_STORAGE permission enabled.

Why are you trying to write a file in external storage?

I mean if you want this file for your app only means, use context.getExternalDirs() to get your app's sandbox, it doesn't require write permission, above android 4.2(Jelly bean).

If you want to share the file to other apps, you're doing the right job.

And before writing the file, check whether external storage is mounted programmatically.

over 4 years ago · Santiago Trujillo Denunciar

0

Problem Solved

I used SharedPreferences of Android. I stored the data in MainActivity and take in in my Class as follow:

  • MainActivity

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("string_id", InputString); //InputString: from the EditText
    editor.commit();
    
  • In my Class to get my data

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String data = prefs.getString("string_id", "no id"); //no id: default value
    
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda