Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence File Storage Writing to Internal Storage

Eston Burciaga
Eston Burciaga
1,729 Points

Mememaker: What is the method that checks if a file already exisits?

I am doing a task for the MemeMaker module and am having a problem trying to solve the task that ask you to fix the code so that it only opens a file if the file doesn't already exist.

I looked at the Oracle's Java documentation for the File class and I found the "createNewFile" method placed on to the end of the "new File(string, string) call and it gave me an error something like the createNewFile symbol can't be found.

I'm confused... why can't the createNewFile method be found if it exists in the File class?

import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileUtilities {

  public static boolean copyResult;

  public static void saveAssetImage(Context context, String assetName) {
    File fileToWrite = new File(context.getFilesDir(), assetName);
    AssetManager assetManager = context.getAssets();

    try {
      InputStream in = assetManager.open(assetName);
      FileOutputStream out = new FileOutputStream(fileToWrite).createNewFile;
      copyResult = copyFile(in, out);
    } catch(FileNotFoundException e) {
    } catch (IOException e) {

  private static boolean copyFile(InputStream in, FileOutputStream out) {
    // Copy magic intentionally omitted
           byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != 1) {
            out.write(buffer, 0, read);

    return true;

3 Answers


Yes, the challenge is looking for:

          copyResult = copyFile(in, out);

The if statement is added round the line that triggers the copy method.


You could try adding something like

if (!file.exists()){
    // do something to create a file
  } else {
// it does exist
Eston Burciaga
Eston Burciaga
1,729 Points

Thanks for your help , guys. It looks like it will fix my problem. I will give it a try.