Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.7K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda