Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Eston Burciaga
1,729 PointsMememaker: 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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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

Steve Hunter
57,684 PointsHI,
Yes, the challenge is looking for:
if(!fileToWrite.exists()){
copyResult = copyFile(in, out);
}
The if
statement is added round the line that triggers the copy method.
Steve.

Steve Hunter
57,684 PointsYou could try adding something like
if (!file.exists()){
// do something to create a file
} else {
// it does exist
}

Eston Burciaga
1,729 PointsThanks for your help , guys. It looks like it will fix my problem. I will give it a try.