Welcome to the Treehouse Community
Looking to learn something new?
Kevin Faust15,353 Points
Why do we create a temporary file instead of a a normal file?
Ben creates a file like this:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String fileName = "IMG_" + timeStamp; String fileType = ".jpg"; //create the file File imageFile = null; imageFile = File.createTempFile(fileName, fileType, mediaStorageDir);
How come Ben didn't create the file like this?:
imageFile = new File(mediaStorageDir, fileName + fileType); imageFile.createNewFile();
I searched online and some have said they use createTempFile because it appends some random numbers to the end of the file name which makes the file name unique. Since Ben names the file using the current time, that makes an automatically unique file name. I am wondering whether there was a special reason Ben used the first code example above and which one is a better way to create files.