/** * @param f the file to copy * @param destDir the destination directory */ public static void copyImageFile(File f, String destDir) { FileChannel src = null; FileChannel dest = null; try { File destFile = new File(destDir + File.separator + f.getName()); if (!destFile.exists()) { destFile.createNewFile(); } else if (f.lastModified() <= destFile.lastModified()) { return; } src = new FileInputStream(f).getChannel(); dest = new FileOutputStream(destFile).getChannel(); dest.transferFrom(src, 0, src.size()); } catch (IOException e) { System.err.println(e); } finally { try { if (src != null) { src.close(); } if (dest != null) { dest.close(); } } catch (IOException e) { System.err.println(e); } } }
private static final void copyFile(File source, File destination) throws IOException { FileChannel sourceChannel = new FileInputStream(source).getChannel(); FileChannel targetChannel = new FileOutputStream(destination).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), targetChannel); sourceChannel.close(); targetChannel.close(); }
public StorageRootFile(String dbPath, String options, int pageSize, FreeSpaceManager fsm) { this.fsm = fsm; PAGE_SIZE = pageSize; File file = new File(dbPath); if (!file.exists()) { throw DBLogger.newUser("DB file does not exist: " + dbPath); } try { raf = new RandomAccessFile(file, options); fc = raf.getChannel(); try { // tryLock is supposed to return null, but it throws an Exception fileLock = fc.tryLock(); if (fileLock == null) { fc.close(); raf.close(); throw DBLogger.newUser("This file is in use by another process: " + dbPath); } } catch (OverlappingFileLockException e) { fc.close(); raf.close(); throw DBLogger.newUser("This file is in use by another PersistenceManager: " + dbPath); } if (ZooDebug.isTesting()) { ZooDebug.registerFile(fc); } } catch (IOException e) { throw DBLogger.newFatal("Error opening database: " + dbPath, e); } }
/** * Copy source file to output file * * @param File sourceFile - source file * @param File outputFile - output file * @return long - CRC value */ public synchronized String copyFile(File sourceFile, File outputFile) { String result = null; FileChannel sourceChannel = null; FileChannel outputChannel = null; try { sourceChannel = (new FileInputStream(sourceFile)).getChannel(); outputChannel = (new FileOutputStream(outputFile)).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), outputChannel); } catch (Exception e) { result = AppConstanst.CAN_NOT_COPY + sourceFile.getPath() + AppConstanst.COPY_TO + outputFile.getPath() + AppConstanst.CAN_NOT_COPY_REASON + e.getMessage(); } finally { try { sourceChannel.close(); outputChannel.close(); } catch (Exception ioExp) { result = AppConstanst.CAN_NOT_CLOSE_CHANNEL + ioExp.getMessage(); } } if (result == null) { result = AppConstanst.COPPING_FILE + sourceFile.getPath() + AppConstanst.COPY_TO + outputFile.getPath() + AppConstanst.COPY_SUCCESS; } return result; }
private void copyFileToTempDir(String file, String name, String loc) { File j = new File(file); if (j.exists()) { FileChannel source = null; FileChannel destination = null; File jNew = new File(loc + name); if (!jNew.exists()) { try { jNew.createNewFile(); source = new FileInputStream(j).getChannel(); destination = new FileOutputStream(jNew).getChannel(); destination.transferFrom(source, 0, source.size()); if (source != null) { source.close(); } if (destination != null) { destination.close(); } } catch (IOException ex) { Logger.getLogger(JarBuilderInvoker.class.getName()).log(Level.SEVERE, null, ex); } } } }
private void seizeInternal() { FileChannel src = null; FileChannel dest = null; try { File srcFile = new File(mmPath); File destFile = new File(mContext.getExternalFilesDir(null), srcFile.getName()); src = new FileInputStream(srcFile).getChannel(); dest = new FileOutputStream(destFile).getChannel(); dest.transferFrom(src, 0, src.size()); mmPath = destFile.getAbsolutePath(); } catch (IOException e) { throw new RuntimeException(e); } finally { try { if (src != null) { src.close(); } } catch (IOException e) { } try { if (dest != null) { dest.close(); } } catch (IOException e) { } } }
public static long copyFile(java.nio.file.Path srcfile, java.nio.file.Path dstfile) throws java.io.IOException { java.nio.channels.FileChannel inchan = java.nio.channels.FileChannel.open(srcfile, OPENOPTS_READ); java.nio.channels.FileChannel outchan = null; long nbytes = 0; long filesize; try { filesize = inchan.size(); outchan = java.nio.channels.FileChannel.open(dstfile, OPENOPTS_CREATE_WRITE_TRUNC); nbytes = outchan.transferFrom(inchan, 0, inchan.size()); } finally { try { inchan.close(); } catch (Exception ex) { } if (outchan != null) try { outchan.close(); } catch (Exception ex) { } } if (nbytes != filesize) throw new java.io.IOException( "file-copy=" + nbytes + " vs " + filesize + " for " + srcfile + " => " + dstfile); return nbytes; }
public boolean backupConfig(@NonNull String sourceString, @NonNull String destString) { try { File sourceFile = new File(sourceString); File destFile = new File(destString); if (!sourceFile.exists()) return false; if (!destFile.exists()) destFile.createNewFile(); FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) source.close(); if (destination != null) destination.close(); } if (source != null) source.close(); if (destination != null) destination.close(); } catch (IOException ex) { Warning.load("Cannot backup config: " + sourceString, false); return false; } return true; }
public static void moveDBtoSD(String name) { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//org.questionairemanager.engine//databases//" + name; String backupDBPath = name; Log.d("DEBUG", backupDBPath); File currentDB = new File(data, currentDBPath); 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) { Log.d("SD", "Error moviendo la db error:" + e.getMessage()); } }
/** * 文件复制 * * @param s * @param t */ public static void fileCopy(File s, File t) { t.delete(); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel(); // 得到对应的文件通道 out = fo.getChannel(); // 得到对应的文件通道 in.transferTo(0, in.size(), out); // 连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
private boolean loadFromFile() { File GeoCrc = new File(fileName); if (!GeoCrc.exists()) { return false; } try { FileChannel roChannel = new RandomAccessFile(GeoCrc, "r").getChannel(); if (roChannel.size() != GeoEngine.BLOCKS_IN_MAP * 4) { roChannel.close(); return false; } ByteBuffer buffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, roChannel.size()); roChannel.close(); buffer.order(ByteOrder.LITTLE_ENDIAN); int[] _checkSums = new int[GeoEngine.BLOCKS_IN_MAP]; for (int i = 0; i < GeoEngine.BLOCKS_IN_MAP; i++) { _checkSums[i] = buffer.getInt(); } checkSums[geoX][geoY] = _checkSums; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
private NPOIFSFileSystem(FileChannel channel, boolean closeChannelOnError) throws IOException { this(false); try { // Get the header ByteBuffer headerBuffer = ByteBuffer.allocate(POIFSConstants.SMALLER_BIG_BLOCK_SIZE); IOUtils.readFully(channel, headerBuffer); // Have the header processed _header = new HeaderBlock(headerBuffer); // Now process the various entries _data = new FileBackedDataSource(channel); readCoreContents(); } catch (IOException e) { if (closeChannelOnError) { channel.close(); } throw e; } catch (RuntimeException e) { // Comes from Iterators etc. // TODO Decide if we can handle these better whilst // still sticking to the iterator contract if (closeChannelOnError) { channel.close(); } throw e; } }
public static void main(String[] args) throws IOException { System.err.println(df.format(new Date(System.currentTimeMillis()))); File rfile = new File("D:/TDDOWNLOAD/17173_SC2-WingsOfLiberty-zhCN-Installer.rar"); File wfile = new File("E:/17173_SC2-WingsOfLiberty-zhCN-InstallerCopy.rar"); FileChannel rchannel = new FileInputStream(rfile).getChannel(); FileChannel wchannel = new FileOutputStream(wfile).getChannel(); ByteBuffer[] buffers = new ByteBuffer[1024]; for (int i = 0; i < buffers.length; i++) { buffers[i] = ByteBuffer.allocate(1024); } boolean isLoop = true; while (isLoop) { for (ByteBuffer buffer : buffers) { buffer.rewind(); } isLoop = rchannel.read(buffers) != -1L; for (ByteBuffer buffer : buffers) { buffer.flip(); } wchannel.write(buffers); } rchannel.close(); wchannel.close(); }
public static void copyFile(File srcFile, File dstFile) { if (!dstFile.exists()) { try { dstFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(srcFile).getChannel(); destination = new FileOutputStream(dstFile).getChannel(); destination.transferFrom(source, 0, source.size()); } catch (IOException e) { e.printStackTrace(); } finally { if (source != null) { try { source.close(); } catch (IOException e) { e.printStackTrace(); } } if (destination != null) { try { destination.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Function that copies file "from" to file "to". * * @param from - file to copy from. * @param to - file to copy to. * @throws IOException */ private void copyFile(File from, File to) throws IOException { FileChannel in = (new FileInputStream(from)).getChannel(); FileChannel out = (new FileOutputStream(to)).getChannel(); in.transferTo(0, from.length(), out); in.close(); out.close(); }
public void bt_resetAction(View v) { // Log.d( SystemInfo.TIG, TAG + "::bt_resetAction() - RESET DATABASE" ); // TODO implement option to dump db data on SD card try { // TODO this code is not enough tested - it shall rather show the idea!!! File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + SystemInfo.TIG + ".db" + "//databases//" + SystemInfo.DB_NAME; String backupDBPath = SystemInfo.DB_NAME; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, backupDBPath); if (currentDB.exists()) { fileInputStream = new FileInputStream(currentDB); FileChannel src = fileInputStream.getChannel(); fileOutputStream = new FileOutputStream(backupDB); FileChannel dst = fileOutputStream.getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } } } catch (Exception e) { Log.w(SystemInfo.TIG, TAG + " saving database failed"); } // reset DB (dialog) AlertDialog.Builder yesnobox = new AlertDialog.Builder(this); yesnobox.setMessage("Really remove all data?"); yesnobox.setTitle("Confirm Database Reset"); yesnobox.setPositiveButton( "Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for (int idx = 0; idx < DB_list.size(); ++idx) { DB_list.get(idx).data_reset(); } // TODO rm /* DB.data_reset(); DB.data_reset(); // XXX DB.data_reset(); DB.data_reset(); //*/ // refresh to battery view displayData(SystemInfo.PLOT_BATTERY_COMMENT, SystemInfo.DB_TABLENAME_BATTERY); } }); yesnobox.setNegativeButton( "No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do nothing ; } }); yesnobox.show(); }
private void copy(String here, String to) { // TODO Auto-generated method stub File s = new File(here); File t = new File(to); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel(); // 得到对应的文件通道 out = fo.getChannel(); // 得到对应的文件通道 in.transferTo(0, in.size(), out); // 连接两个通道,并且从in通道读取,然后写入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** @tests java.nio.channels.FileLock#release() */ public void test_release() throws Exception { File file = File.createTempFile("test", "tmp"); file.deleteOnExit(); FileOutputStream fout = new FileOutputStream(file); FileChannel fileChannel = fout.getChannel(); FileLock fileLock = fileChannel.lock(); fileChannel.close(); try { fileLock.release(); fail("should throw ClosedChannelException"); } catch (ClosedChannelException e) { // expected } // release after release fout = new FileOutputStream(file); fileChannel = fout.getChannel(); fileLock = fileChannel.lock(); fileLock.release(); fileChannel.close(); try { fileLock.release(); fail("should throw ClosedChannelException"); } catch (ClosedChannelException e) { // expected } }
public void copy() { try { File sd = AndroidSensors.DataPath; File data = Environment.getDataDirectory(); if (sd.exists() == false) sd.mkdirs(); String currentDBPath = "//data//" + "winlab.sensoradventure" + "//databases//" + "InstantReading.db"; String backupDBPath = "InstantReading.db"; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Message msg = handler.obtainMessage(); msg.arg1 = 1; handler.sendMessage(msg); } catch (Exception e) { Message msg = handler.obtainMessage(); msg.arg1 = 2; handler.sendMessage(msg); } }
// from https://gist.github.com/889747 // TODO use Jakarta Commons IO..! This implementation needs to be improved. private static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileInputStream fIn = null; FileOutputStream fOut = null; FileChannel source = null; FileChannel destination = null; try { fIn = new FileInputStream(sourceFile); source = fIn.getChannel(); fOut = new FileOutputStream(destFile); destination = fOut.getChannel(); long transfered = 0; long bytes = source.size(); while (transfered < bytes) { transfered += destination.transferFrom(source, 0, source.size()); destination.position(transfered); } } finally { if (source != null) { source.close(); } else if (fIn != null) { fIn.close(); } if (destination != null) { destination.close(); } else if (fOut != null) { fOut.close(); } } }
public static BackupResult simpleBackup(Context context, String appName) { BackupResult result = new BackupResult(); String dbName = ODKFileUtils.getBackupFolder(appName) + generateFileName(context); result.setDatabaseFileName(dbName); FileInputStream fromFile = null; FileOutputStream toFile = null; FileChannel from = null; FileChannel to = null; try { fromFile = new FileInputStream(ODKFileUtils.getCensusDbFullPath(appName)); toFile = new FileOutputStream(dbName); from = fromFile.getChannel(); to = toFile.getChannel(); from.transferTo(0, from.size(), to); result.setSuccess(true); } catch (FileNotFoundException e) { result.setSuccess(false); e.printStackTrace(); } catch (IOException e) { result.setSuccess(false); e.printStackTrace(); } finally { if (fromFile != null) { try { fromFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (toFile != null) { try { toFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (from != null) { try { from.close(); } catch (IOException e) { e.printStackTrace(); } } if (to != null) { try { to.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
public static void copyFile(File src, File dst) throws IOException { dst.createNewFile(); FileChannel in = new FileInputStream(src).getChannel(); FileChannel out = new FileOutputStream(dst).getChannel(); out.transferFrom(in, 0, in.size()); in.close(); out.close(); }
public static String copyWSASWar(IProgressMonitor monitor, String wsasHome) throws FileNotFoundException, IOException { String tempWarFile = null; String tempWarLocation = null; String tempUnzipLocation = null; try { if (new File(WSASCoreUtils.tempWSASDirectory()).isDirectory()) { tempWarLocation = WSASCoreUtils.addAnotherNodeToPath( WSASCoreUtils.tempWSASDirectory(), WSASCoreUIMessages.DIR_TEMPWAR); File tempWarLocationFile = new File(tempWarLocation); if (tempWarLocationFile.exists()) { FileUtils.deleteDirectories(tempWarLocationFile); } tempWarLocationFile.mkdirs(); tempWarFile = WSASCoreUtils.addAnotherNodeToPath(tempWarLocation, WSASCoreUIMessages.FILE_WSAS_WAR); new File(tempWarFile).createNewFile(); Properties properties = new Properties(); // properties.load(new FileInputStream(WSASCoreUtils.tempWSASWebappFileLocation())); // if (properties.containsKey(WSASCoreUIMessages.PROPERTY_KEY_PATH)){ if (ServerModel.getWsasServerPath() != null) { String wsasWarFile = WSASCoreUtils.addAnotherNodeToPath( (ServerModel.getWsasServerPath() != null) ? ServerModel.getWsasServerPath() : properties.getProperty(WSASCoreUIMessages.PROPERTY_KEY_PATH), WSASCoreUIMessages.FILE_WSAS_WAR); FileChannel srcChannel = new FileInputStream(wsasWarFile).getChannel(); FileChannel dstChannel = new FileOutputStream(tempWarFile).getChannel(); // Copy file contents from source to destination dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); // Close the channels srcChannel.close(); dstChannel.close(); // unzip this into another foulder tempUnzipLocation = FileUtils.addAnotherNodeToPath(tempWarLocation, WSASCoreUIMessages.DIR_UNZIP); File tempUnzipLocationFile = new File(tempUnzipLocation); if (!tempUnzipLocationFile.exists()) { tempUnzipLocationFile.mkdirs(); } unzipWSASWar(tempWarFile, tempUnzipLocation); } } else { // Throws an error message } } catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } return tempUnzipLocation; }
void copyFileLeak(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
/** * コピー元のパス[srcPath]から、コピー先のパス[destPath]へファイルのコピーを行います。 コピー処理にはFileChannel#transferToメソッドを利用します。 * コピー処理終了後、入力・出力のチャネルをクローズします。 * * @param srcPath コピー元のパス * @param destPath コピー先のパス * @throws IOException 何らかの入出力処理例外が発生した場合 */ public static void copyTransfer(String srcPath, String destPath) throws IOException { FileChannel srcChannel = new FileInputStream(srcPath).getChannel(); FileChannel destChannel = new FileOutputStream(destPath).getChannel(); try { srcChannel.transferTo(0, srcChannel.size(), destChannel); } finally { srcChannel.close(); destChannel.close(); } }
private void copyFile(File sourceFile, File destFile, Reporter reporter) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; Throttler throttler = new Throttler((double) bytesPerSecThrottle); FileInputStream iS = null; FileOutputStream oS = null; try { iS = new FileInputStream(sourceFile); oS = new FileOutputStream(destFile); source = iS.getChannel(); destination = oS.getChannel(); long bytesSoFar = 0; long reportingBytesSoFar = 0; long size = source.size(); int transferred = 0; while (bytesSoFar < size) { // Casting to int here is safe since we will transfer at most "downloadBufferSize" bytes. // This is done on purpose for being able to implement Throttling. transferred = (int) destination.transferFrom(source, bytesSoFar, downloadBufferSize); bytesSoFar += transferred; reportingBytesSoFar += transferred; throttler.incrementAndThrottle(transferred); if (reportingBytesSoFar >= bytesToReportProgress) { reporter.progress(reportingBytesSoFar); reportingBytesSoFar = 0l; } } if (reporter != null) { reporter.progress(reportingBytesSoFar); } } finally { if (iS != null) { iS.close(); } if (oS != null) { oS.close(); } if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
public void JSONActions() { try { actions.put("video-finished-playing", action); events.put("actions", actions); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } FileChannel src = null, des = null; try { File file = new File(destinationPath + "/videos"); File desFilePath = new File(sourcePath); String desPath = desFilePath.getName(); File desFile = new File(destinationPath + "/videos/" + desPath); if (!desFile.exists()) { if (file.isDirectory()) { src = new FileInputStream(sourcePath).getChannel(); des = new FileOutputStream(destinationPath + "/videos/" + desPath).getChannel(); } else { file.mkdir(); src = new FileInputStream(sourcePath).getChannel(); des = new FileOutputStream(destinationPath + "/videos/" + desPath).getChannel(); } } try { if (des != null) des.transferFrom(src, 0, src.size()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (src != null) { src.close(); des.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
private static void copyFile(File in, File out) throws IOException { FileChannel inCh = new FileInputStream(in).getChannel(); FileChannel outCh = new FileOutputStream(out).getChannel(); try { inCh.transferTo(0, inCh.size(), outCh); } catch (IOException e) { throw e; } finally { if (inCh != null) inCh.close(); if (outCh != null) outCh.close(); } }
private static void copyFile(File source, File dest) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { inputChannel.close(); outputChannel.close(); } }
private void copyFile(File sourceFile, File targetFile) throws IOException { FileChannel inChannel = new FileInputStream(sourceFile).getChannel(); FileChannel outChannel = new FileOutputStream(targetFile).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }