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

private static byte[] readFile2Bytes(final File file) {
    FileChannel fc = null;
    try {/*from   w ww .j a v a 2 s .c o m*/
        fc = new RandomAccessFile(file, "r").getChannel();
        int size = (int) fc.size();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
        byte[] data = new byte[size];
        mbb.get(data, 0, size);
        return data;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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   w w  w.j a va  2 s .c  om*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        outChannel.close();
    }

    in.close();
    out.close();
}

From source file:Main.java

public static ByteBuffer readBytes(String file) throws IOException {
    ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
    FileChannel fChannel = new RandomAccessFile(file, "r").getChannel();
    fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));
    fChannel.close();/*from www.ja  va2 s.c o  m*/
    return ByteBuffer.wrap(dataOut.toByteArray());
}

From source file:Main.java

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();/*from  w w  w.java2 s  .c  o  m*/
    outStream.flush();
    outStream.close();
}

From source file:com.viddu.handlebars.HandlebarsFileUtil.java

/**
 * Utility method to get contents of the File.
 * /*www  .  j  a v  a2 s .  c om*/
 * @param inputFile
 * @return
 * @throws IOException
 */
public static String getFileContents(File inputFile) throws IOException {
    FileInputStream stream = new FileInputStream(inputFile);
    try {
        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:GrepNIO.java

static void process(Pattern pattern, String fileName) throws IOException {

    // Get a FileChannel from the given file.
    FileChannel fc = new FileInputStream(fileName).getChannel();

    // Map the file's content
    ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    // Decode ByteBuffer into CharBuffer
    CharBuffer cbuf = Charset.forName("ISO-8859-1").newDecoder().decode(buf);

    Matcher m = pattern.matcher(cbuf);
    while (m.find()) {
        System.out.println(m.group(0));
    }//from   w w  w.  j  av  a  2 s .  c  om
}

From source file:org.geomajas.plugin.deskmanager.utility.FileUtils.java

/**
 * Zip all files in a directory, only relative filenames are used, does not recurse.
 * //from  w w w . j a v  a  2  s.co  m
 * @param sourceFolder
 * @param targetFileName
 * @return
 * @throws IOException
 */
public static File zipDirectory(File sourceFolder, String targetFileName) throws IOException {
    File target = new File(sourceFolder, targetFileName + (targetFileName.endsWith(".zip") ? "" : ".zip"));
    if (target.isFile()) {
        throw new IOException("Bestand bestaat reeds! " + target.getAbsolutePath());
    }
    File[] files = sourceFolder.listFiles();

    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(target));
    WritableByteChannel out = Channels.newChannel(zos);
    FileInputStream is = null;
    try {
        for (File file : files) {
            zos.putNextEntry(new ZipEntry(file.getName()));

            is = new FileInputStream(file);
            FileChannel in = is.getChannel();
            in.transferTo(0, in.size(), out);

            is.close();
        }
    } catch (IOException e) {
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
        if (zos != null) {
            zos.close();
        }
    }

    return target;
}

From source file:Main.java

/**
 * Copies the contents of one file to the other using {@link FileChannel}s.
 * //w w  w  .j  ava  2s. com
 * @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 String findTitle(File f) {
    if (titlePattern == null) {
        initPattern();//from   www  .ja v a2  s  . c om
    }

    try {
        FileChannel fc = new FileInputStream(f).getChannel();

        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

        CharBuffer cb = Charset.forName("8859_1").newDecoder().decode(bb); //$NON-NLS-1$

        Matcher m = titlePattern.matcher(cb);
        String title = null;
        if (m.find()) {
            title = m.group(1);
        }

        return title;
    } catch (IOException e) {
        return null;
    }
}

From source file:GrepSun.java

private static void grep(File f) throws IOException {

    // Open the file and then get a channel from the stream
    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();

    // Get the file's size and then map it into memory
    int sz = (int) fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);

    // Decode the file into a char buffer
    CharBuffer cb = decoder.decode(bb);

    // Perform the search
    grep(f, cb);/*  w  w  w  . ja  va  2s  . co m*/

    // Close the channel and the stream
    fc.close();
}