List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:Main.java
/** * Copies the contents of one file to the other using {@link FileChannel}s. * //from w w w. j a va 2 s .co m * @param src * source {@link File} * @param dst * destination {@link File} */ public static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } in.close(); out.close(); }
From source file:Main.java
public static void restoreDb(File currentDB, String restoreDBFileName) { try {/*from w w w . jav a2s. co m*/ backupDb(currentDB); File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { File f = new File(sd.getAbsolutePath() + "/AccountManagement"); if (!f.exists()) { f.mkdir(); } String restoreDBPath = "AccountManagement/" + restoreDBFileName; File restoreDB = new File(sd, restoreDBPath); if (currentDB.exists()) { FileChannel src = new FileInputStream(restoreDB).getChannel(); FileChannel dst = new FileOutputStream(currentDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } } } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void copyFdToFile(FileDescriptor src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {/*w w w .j ava2s . c om*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } }
From source file:Main.java
private static void copyFile(File src, File dst) throws IOException { FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); FileChannel fromChannel = null, toChannel = null; try {/*ww w .j a v a 2s . c om*/ fromChannel = in.getChannel(); toChannel = out.getChannel(); fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { if (fromChannel != null) fromChannel.close(); if (toChannel != null) toChannel.close(); } }
From source file:Main.java
public static void backupDb(File currentDB) { try {// ww w . j a va 2s .c o m File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { File f = new File(sd.getAbsolutePath() + "/AccountManagement"); if (!f.exists()) { f.mkdir(); } Date currentDate = new Date(); String currentDateStr = newFormat1.format(currentDate); currentDateStr = currentDateStr.trim(); System.out.println( "---------------------------------------------------------------" + currentDateStr); String backupDBPath = "AccountManagement/backup_" + currentDateStr + ".db"; File backupDB = new File(sd, backupDBPath); if (currentDB.exists()) { FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } } } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
private static void copyFile(File src, File dst) throws Exception { FileChannel inChannel = (new FileInputStream(src)).getChannel(); FileChannel outChannel = (new FileOutputStream(dst)).getChannel(); try {// w w w.j a v a 2s. com inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {/* w ww . j a v a2 s . c o m*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } }
From source file:Main.java
public static byte[] readFileToMemory(String file) throws IOException { FileInputStream fis = new FileInputStream(file); FileChannel fileChannel = fis.getChannel(); long size = fileChannel.size(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size); byte[] data = new byte[(int) size]; mappedByteBuffer.get(data, 0, (int) size); fileChannel.close(); fis.close();//from ww w.j a v a 2s .c om return data; // return readFileToMemory(new File(file)); }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {/*from ww w. j a v a 2 s . c om*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); outChannel.close(); } } }
From source file:Main.java
public static void copyFile(@NonNull String pathFrom, @NonNull String pathTo) throws IOException { FileChannel outputChannel = null; FileChannel inputChannel = null; try {//from w ww .j av a 2 s.co m inputChannel = new FileInputStream(new File(pathFrom)).getChannel(); outputChannel = new FileOutputStream(new File(pathTo)).getChannel(); inputChannel.transferTo(0, inputChannel.size(), outputChannel); inputChannel.close(); } finally { if (inputChannel != null) inputChannel.close(); if (outputChannel != null) outputChannel.close(); } }