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
J Smillie
Courses Plus Student 40,123 PointsStuck on Android Data Persistence Question
Can someone show me what I should be doing here?
I thought the question was relevant to the code written in the video preceding this challenge but I can't seem to figure out how.
The following code is similar to what you just wrote. Modify this code to only write the file if it does not already exist. Hint: Use the method from the File class that checks to see if a file exists or not.
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);
copyResult = copyFile(in, out);
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean copyFile(InputStream in, FileOutputStream out) throw IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
// Copy magic intentionally omitted
return true;
}
}
'''
J Smillie
Courses Plus Student 40,123 PointsHi Ken,
Nio I didn't! I decided to switch courses and come back to this...
2 Answers
Ken Alger
Treehouse TeacherJonathan;
Task
The following code is similar to what you just wrote. Modify this code to only write the file if it does not already exist. Hint: Use the method from the File class that checks to see if a file exists or not.
Planning
It looks like we need to alter our code where we are writing to the file, correct? That is this line here (line 16 in a new challenge):
copyResult = copyFile(in, out);
We need to make sure that we are only doing that if the file does not already exist, so we need to make some alterations.
Performing
As the instruction state, there is a method we can utilize and set it to false, since then the file does not exist. It would look something like:
fileToWrite.exists() == false
Now we need to add a conditional to it so that if the file doesn't exist, we copy our result to the file. That would look something along the lines of:
if (fileToWrite.exists() == false) { copyResult = copyFile(in, out); }
Make any sense? I hope it helps and happy coding.
Ken
Alwin Lazar V
5,267 PointsI have solved and it works for me
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);
// here I changed
if (fileToWrite.exists() == false) { 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 return true;
} }
Happy coding
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherJonathan;
Did you get this issue resolved?
Ken