Gee Boughuski
Gee Boughuski

Reputation: 53

Android: File Not Found Exception

I am trying to program a simple todo app for my android phone. Ive gotten far enough that I would like to save these strings that I input. However, every time I try to write the data I get a file not found exception. Here is the code I use in my onCreate method to instantiate the File.

File path = getFilesDir();
File itemFile = new File(path,"Todo_File.txt");

I then have two methods, one to write to the File, and the other to read from it. They look like this:`

public void readItems() {
    try {
        BufferedReader reader = new BufferedReader(new FileReader("Todo_File.txt"));
        while(reader.readLine()!=null){
            items.add(reader.readLine());
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}

and

public void writeItems() {
    try{
        BufferedWriter writer = new BufferedWriter(new FileWriter("Todo_File.txt"));
        for(int i=0;i<items.size();i++){
            writer.write(items.get(i));
        }
    } catch (IOException e){
        e.printStackTrace();
    }
}

items is a stringArray which holds the strings that were input. Every time that I try to write or read the files I get the following exception:

W/System.err: java.io.FileNotFoundException: Todo_File.txt (No such file or directory)

I don't understand why Android Studio cant find the file that I created, can anyone help?

Upvotes: 1

Views: 9892

Answers (4)

Marco de Abreu
Marco de Abreu

Reputation: 663

The standard way to do File-IO in Android is using the context-relevant IO-methods.

To write a file, use the following code. Details about the different file-modes are available here.

FileOutputStream fOut = openFileOutput("Todo_File.txt", MODE_WORLD_READABLE);

To read a file, use this:

FileInputStream fIn = openFileInput("Todo_File.txt");

Upvotes: 1

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

You need to actually create the file before you write to it. You should do something like this:

File path = getFilesDir();  
File itemFile = new File(path,"Todo_File.txt");

if (!path.exists()) {
     path.mkdirs();
}

What you have done is simply tried to read from the file. The fact that you get the error:

W/System.err: java.io.FileNotFoundException: Todo_File.txt (No such file or directory)

Is an indication that the file you want to write to hasn't been created, and I don't see you creating the file anywhere.

If you are using an emulator, you need to make sure that you have an SDK card set up on your device, and then do:

File itemFile = new File(Environment.getExternalStorageDirectory(),"Todo_File.txt");

if (!path.exists()) {
     path.mkdirs();
}

Upvotes: 0

nandsito
nandsito

Reputation: 3852

Since you defined

File path = getFilesDir();  
File itemFile = new File(path,"Todo_File.txt");

you can do:

public void readItems() {
    try {
        BufferedReader reader = new BufferedReader(new FileReader(itemFile));
        while(reader.readLine()!=null){
            items.add(reader.readLine());
        }
    } catch(IOException e) {
        e.printStackTrace();
    }
}
public void writeItems() {
    try{
        BufferedWriter writer = new BufferedWriter(new FileWriter(itemFile));
        for(int i=0;i<items.size();i++){
            writer.write(items.get(i));
        }
    } catch (IOException e){
        e.printStackTrace();
    }
}

Upvotes: 0

S G
S G

Reputation: 802

You are looking for a file "Todo_File.txt".

Where have you kept this file? Are you keeping it as a resource file in the "res/raw" directory of your app or it is lying somewhere in the phone storage?

Here you can get some idea of types of the storage

https://developer.android.com/guide/topics/data/data-storage.html

https://android.stackexchange.com/questions/112951/two-types-of-internal-storage-what-is-the-difference

Mostly likely I guess you need to correct the path of this file. here are the way to get the "/storage/sdcard0/" path

Environment.getExternalStorageDirectory()

Upvotes: 1

Related Questions