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 trialFernando Zegarra
Courses Plus Student 2,596 PointsI cant solved the code exercise "Writing to internal Storage"
I practically copy and paste the code asked for this excercice
throws IOException { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); }
but they always say the same,cant find symbol "write" anyone have faced this issue ??
5 Answers
David Anton
Courses Plus Student 30,936 PointsThe challenge is: "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."
So you only have to add method to check if the file exists or not, if exists return and dont copy the file:
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();
// START YOUR CODE HERE
if(fileToWrite.exists()){
return;
}
// END YOUR CODE
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) {
// Copy magic intentionally omitted
return true;
}
}
Fernando Zegarra
Courses Plus Student 2,596 PointsThanks for your quicly answer , I apreciate your support, I was reading bad, a little ashamed
Karan Kumar
3,830 PointsThis worked for me, but how did you know you were supposed to use the exists() method? In the videos I was following, it was never mentioned.
Fernando Zegarra
Courses Plus Student 2,596 PointsHi David: Of course import java.io.InputStream; import java.io.FileNotFoundException; import java.io.IOException;
I tried adding "import java.io.FileOutputStream;" but it throw another error;
thanks
David Anton
Courses Plus Student 30,936 PointsI suggest you to use efficient library to do all your IO actions. IOUtils from apache
add this to your gradle:
compile 'org.apache.commons:commons-io:1.3.2'
and you simply write to streams by calling IOUtils.copy(inputStream, outputStream)
Example:
InputStream in = ...
OutputStream out = ...
try{
IOUtils.copy(in, out);
}catch(IOException e){
}finally{
//close you streams
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
Fernando Zegarra
Courses Plus Student 2,596 PointsHi David Thanks for your answer and time, dont know when I really face with this real problem. thanks anyway What I meant was the solution of Challenge Task 1 of 1 of "writing-to-internal-storage" for Androdi Data persistence
Fernando Zegarra
Courses Plus Student 2,596 PointsYEs that one, as you see it s no possible to add a graddle
Fernando Zegarra
Courses Plus Student 2,596 PointsThanks for your quicly answer , I apreciate your support, I was reading bad, a little ashamed
David Anton
Courses Plus Student 30,936 PointsWelcome, if this answers helped you please mark is as Best Answer.
David Anton
Courses Plus Student 30,936 PointsDavid Anton
Courses Plus Student 30,936 PointsCan you please attach your imports list here?