import java.io.File;
import java.io.FileFilter;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.logging.Level;
dir.mkdir();
}
- File dest = null;
- File src = null;
for (String s : StringResources.SD_CARD) {
- dest = new File(FilePathResources.getSDCardPath()
- + File.separator + s);
- if (!dest.exists()) {
- src = new File(FilePathResources.getBaseSdcardPath()
- + File.separator +s);
- if (!src.exists()) {
- EMLogger.getLogger().log(Level.WARNING,
- "Failed to create SD card image.! + StringResources.NEW_LINE"
- + "File does not exist :" + src.getPath());
- continue;
- }
-
- try {
- dest.createNewFile();
- } catch (IOException e1) {
- EMLogger.getLogger().log(Level.WARNING,
- "Failed to create SD card image!" + StringResources.NEW_LINE + e1.getMessage());
- continue;
- }
+ Path source = Paths.get(FilePathResources.getBaseSdcardPath(), s);
+ Path target = Paths.get(FilePathResources.getSDCardPath(), s);
- FileInputStream inputStream = null;
- FileOutputStream outputStream = null;
- FileChannel fcin = null;
- FileChannel fcout = null;
+ if (!target.toFile().exists()) {
try {
- inputStream = new FileInputStream(src);
- outputStream = new FileOutputStream(dest);
- fcin = inputStream.getChannel();
- fcout = outputStream.getChannel();
- long size = fcin.size();
- long transferredSize = fcin.transferTo(0, size, fcout);
- if(size != transferredSize) {
- EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
- + StringResources.NEW_LINE + "Copy failed.");
- dest.delete();
- continue;
- }
- } catch (FileNotFoundException e) {
+ Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e2) {
EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
- + StringResources.NEW_LINE + e.getMessage());
- dest.delete();
- continue;
- } catch (IOException e) {
- EMLogger.getLogger().log(Level.WARNING, "Failed to create SD card image!"
- + StringResources.NEW_LINE + e.getMessage());
- dest.delete();
- continue;
- }finally {
- try {
- if (fcout != null) {
- fcout.close();
- }
- if (fcin != null) {
- fcin.close();
- }
- if (outputStream != null) {
- outputStream.close();
- }
- if (inputStream != null) {
- inputStream.close();
- }
- } catch (IOException e) {
- EMLogger.getLogger().log(Level.WARNING, e.getMessage());
- }
+ + StringResources.NEW_LINE + e2.getMessage());
}
}
}
}
-
}