Take the 2-minute tour ×

I'm very new to java so this may be simple but i couldn't find a way to fix it.

I'm building an app and i need to be able to write the current date to a file. The part I'm having problems with is actually getting the location of the file to write to.

Reading a file and putting all the text on each line into an arraylist i had no problem with using:

Scanner in = new Scanner(MainActivity.class.getResourceAsStream("notes.dat"));

However, when trying to declare a filewriter i tried both:

FileWriter f = new FileWriter(new File("date.dat"));


FileWriter f = new FileWriter(new File(MainActivity.class.getResourceAsStream("date.dat")));

(It works for the Scanner but on FileWriter it gives me the error: The constructor File(InputStream) is undefined):

I've also tried putting the file in various locations such as the src folder and the root android project folder and pointing to it correctly but it is never located.

Whatever i seem to do for trying to find the file using filewriter then writing to it, the file can never be located. I created a java file not linked to an app development project folder and tried putting the date.dat in the same folder as the java file and was able to use the following with no issues:


My question is why does FileWriter seem to work when used on a normal java file not linked to an android project but when used on my project it stops working and how can it be fixed?

share|improve this question

1 Answer 1

When you use .getResource() and friends, the JVM will look up a matching name in its class path. Which means this can be a file in a jar.

In order to be able to read from/write to a file, you therefore should not use it.

Try and create an external directory which you know the location of, and work in this directory.

share|improve this answer

Your Answer


