List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:com.wabacus.util.FileLockTools.java
private static boolean release(FileChannel fc) { try {/*from w ww . java 2 s . c o m*/ if (fc != null) { fc.close(); fc = null; } return true; } catch (Exception e) { System.out.println("FileChannel"); e.printStackTrace(); return false; } }
From source file:Main.java
public static boolean importDatabase(String dbName) { try {//from ww w .j a v a2 s. c om File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + PACKAGENAME + "//databases//" + dbName; String backupDBPath = "/AttendanceManager/" + dbName; File backupDB = new File(data, currentDBPath); File currentDB = 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(); } } catch (Exception e) { return false; } return true; }
From source file:Main.java
public static void saveDBInSdcard(Context pContext, String pDatabaseName) { try {/*from w w w . j ava 2 s . co m*/ File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + pContext.getPackageName() + "//databases//" + pDatabaseName; String backupDBPath = pDatabaseName; 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(); Toast.makeText(pContext, backupDB.toString(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(pContext, e.toString(), Toast.LENGTH_LONG).show(); } }
From source file:Main.java
/** * Create a backup of the database./* w w w. jav a2s. com*/ * * @param context who calls the function. * @param dbName Name of the database to backup. (If empty, radiomap.db is used). */ public static void exportDb(Context context, String dbName) { try { if (dbName.equals("")) dbName = "radiomap.db"; File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + context.getApplicationContext().getPackageName() + "//databases//" + dbName; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, dbName); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Log.e("Utils", e.getMessage()); Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show(); } }
From source file:Main.java
/** * Create a backup of the database./* w ww. ja v a 2 s . c o m*/ * * @param context who calls the function. * @param dbName Name of the database to backup. (If empty, radiomap.db is used). */ public static void importDb(Context context, String dbName) { try { if (dbName.equals("")) dbName = "radiomap.db"; File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + context.getApplicationContext().getPackageName() + "//databases//" + dbName; File backupDB = new File(data, currentDBPath); File currentDB = new File(sd, dbName); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Log.e("Utils", e.getMessage()); Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show(); } }
From source file:Main.java
public static void toFile(ByteBuffer buffer, File file) throws IOException { RandomAccessFile raf = null;/*from w w w. j av a2 s . c o m*/ FileChannel channel = null; try { raf = new RandomAccessFile(file, "rw"); channel = raf.getChannel(); channel.write(buffer); channel.force(false /*metadata*/); channel.close(); raf.close(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { // Ignored. } } if (raf != null) { try { raf.close(); } catch (IOException e) { // Ignored. } } } }
From source file:Main.java
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 {/*from ww w . j a v a 2s.co m*/ inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } outChannel.close(); } in.close(); out.close(); }
From source file:Main.java
/** Copia un fichero. * @param source Fichero origen con el contenido que queremos copiar. * @param dest Fichero destino de los datos. * @throws IOException SI ocurre algun problema durante la copia */ public static void copyFile(final File source, final File dest) throws IOException { if (source == null || dest == null) { throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$ }/*from w w w . j av a 2s .c o m*/ final FileInputStream is = new FileInputStream(source); final FileOutputStream os = new FileOutputStream(dest); final FileChannel in = is.getChannel(); final FileChannel out = os.getChannel(); final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); in.close(); out.close(); is.close(); os.close(); }
From source file:eu.mhutti1.utils.storage.StorageDeviceUtils.java
private static boolean canWrite(File file) { try {/*from w w w .j a va 2 s . c o m*/ RandomAccessFile randomAccessFile = new RandomAccessFile(file + "/test.txt", "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); FileLock fileLock = fileChannel.lock(); fileLock.release(); fileChannel.close(); randomAccessFile.close(); return true; } catch (Exception ex) { return false; } }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void sendFileTest(File source, File target) throws Exception { FileInputStream fis = null;/*from w w w .j av a2 s . c o m*/ FileOutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); FileChannel tChannel = fos.getChannel(); target.createNewFile(); sChannel.transferTo(0, source.length(), tChannel); tChannel.close(); sChannel.close(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } }