Example usage for java.nio.channels FileChannel size

List of usage examples for java.nio.channels FileChannel size

Introduction

In this page you can find the example usage for java.nio.channels FileChannel size.

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

From source file:Main.java

/**
 * Create a backup of the database.// w w w. j  a va2  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 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  .j a  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:com.twitter.ambrose.util.JSONUtil.java

public static String readFile(String path) throws IOException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {/*from   w w w .j a va2 s.co m*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:org.springframework.hateoas.VndErrorsMarshallingTest.java

private static String readFile(org.springframework.core.io.Resource resource) throws IOException {

    FileInputStream stream = new FileInputStream(resource.getFile());

    try {//from   w  ww  .j a v  a  2  s . co m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {/*w ww  . j a v  a 2 s .  c  om*/
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

From source file:Main.java

/**
 * Copies the contents of the file in {@code src} to {@code dst} and then deletes the {@code src} if copy was successful.
 * If the file copy was unsuccessful, the src file will not be deleted.
 * @param src Source file/*from w w  w  .  j a v a  2s  .c  o  m*/
 * @param dst Destination file
 * @throws IOException if an error occurred during the file copy
 */
static void moveFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    try {
        long bytesCopied = inChannel.transferTo(0, inChannel.size(), outChannel);
        if (bytesCopied >= src.length())
            src.delete();
    } finally {
        if (inChannel != null)
            inChannel.close();
        outChannel.close();
    }
}

From source file:edu.isi.webserver.FileUtil.java

public static String readFileContentsToString(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {//from ww w.  j av  a  2 s  . c  o  m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static String readString(String filePath) {
    File file = new File(filePath);
    if (!file.exists())
        return null;

    FileInputStream fileInput = null;
    FileChannel channel = null;
    try {//from   w ww  .j  av a 2  s  .c  om
        fileInput = new FileInputStream(filePath);
        channel = fileInput.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer.array());
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
    } finally {

        if (fileInput != null) {
            try {
                fileInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:org.xlcloud.service.manager.VirtualClusterTemplateManagerImpl.java

private static String readFile(String path) throws IOException, FileNotFoundException {
    FileInputStream stream = new FileInputStream(new File(path));
    try {//from   www  .j  a  v a2  s .  co  m
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static void restoreDb(File currentDB, String restoreDBFileName) {
    try {// ww  w  .j  ava 2 s. 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);
    }
}