Kidades
Kidades

Reputation: 670

Create a text file using PrintWriter

PrintWriter p;
    try {
        p = new PrintWriter(new FileWriter("xp.mdb"));
    } catch (FileNotFoundException e) {
        return;
    } catch (IOException e) {
        return;
    }

    p.println("mytext");
    p.close();

I'm new to Android programming and don't quite understand how writing to files work. I want to create a new file and write some text to it. The above code works on Windows but on Android I just keep getting FileNotFoundException.

How can I make it create the file if it doesn't exist?

Upvotes: 0

Views: 93

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

You need to put the file in internal storage, external storage, or (on Android 4.4+) removable storage. Passing a bare filename does not work on Android.

Upvotes: 3

Related Questions