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:MainClass.java

public static void main(String[] args) {
    int count = 100;

    long[] numbers = new long[count];

    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = i;//ww  w . ja va 2s .c  o  m

    }
    File aFile = new File("data.bin");
    FileOutputStream outputFile = null;
    try {
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();

    int numberWritten = 0;

    while (numberWritten < numbers.length) {
        longBuf.put(numbers, numberWritten, min(longBuf.capacity(), numbers.length - numberWritten));
        buf.limit(8 * longBuf.position());
        try {
            file.write(buf);
            numberWritten += longBuf.position();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }
        longBuf.clear();
        buf.clear();
    }

    try {
        System.out.println("File written is " + file.size() + " bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.txt");
    FileOutputStream outputFile = null;
    try {/*from  w ww.  ja  v a2  s .c  o  m*/
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    DoubleBuffer doubleBuf = buf.asDoubleBuffer();
    buf.position(8);
    CharBuffer charBuf = buf.asCharBuffer();
    for (long prime : primes) {
        String primeStr = "prime = " + prime;
        doubleBuf.put(0, (double) primeStr.length());
        charBuf.put(primeStr);
        buf.position(2 * charBuf.position() + 8);
        LongBuffer longBuf = buf.asLongBuffer();
        longBuf.put(prime);
        buf.position(buf.position() + 8);
        buf.flip();
        try {
            file.write(buf);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        buf.clear();
        doubleBuf.clear();
        charBuf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

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();/*  w ww . ja  v a2s . co  m*/
    fis.close();
    return data;
    //      return readFileToMemory(new File(file));
}

From source file:Main.java

public static void copyFileFast(FileInputStream is, FileOutputStream os) throws IOException {
    FileChannel in = is.getChannel();
    FileChannel out = os.getChannel();
    in.transferTo(0, in.size(), out);
}

From source file:z.tool.util.FileUtil.java

/**
 * ?//w  w w .  jav  a2 s  . co  m
 */
public static void copy(FileInputStream fis, FileOutputStream fos) throws IOException {
    FileChannel channel = fis.getChannel();
    channel.transferTo(0, channel.size(), fos.getChannel());
}

From source file:Main.java

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

From source file:Main.java

public static CharSequence fromFile(String filename) throws IOException {
    FileInputStream fis = new FileInputStream(filename);
    FileChannel fc = fis.getChannel();

    // Create a read-only CharBuffer on the file
    ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
    return cbuf;//from   ww  w  . j  a v a  2 s  . c  o  m
}

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();// w  w w.  ja v  a 2  s . c  o m
    outStream.close();
}

From source file:Main.java

static public String getFileContent(String filename) throws IOException {
    File file = new File(filename);
    FileInputStream stream = new FileInputStream(file);
    try {/*from  www  .  j  a v a 2  s  .c o  m*/
        FileChannel fc = stream.getChannel();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

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

From source file:Main.java

public static void copyFile(File src, File dest) throws IOException {
    FileOutputStream fileOut = null;
    FileInputStream fileIn = null;
    try {//from www  .j  a va  2 s.com
        fileOut = new FileOutputStream(dest);
        fileIn = new FileInputStream(src);
        FileChannel inChannel = fileIn.getChannel();
        FileChannel outChannel = fileOut.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (fileIn != null) {
            fileIn.close();
        }
        if (fileOut != null) {
            fileOut.close();
        }
    }
}