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) throws IOException {

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    inChannel.transferTo(0, inChannel.size(), outChannel);

    inChannel.close();//from   www .  ja v a2  s.c om
    outChannel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");

    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
    ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);//from   ww  w.  ja  va 2s. c  o  m
    for (int i = 1; i < 10; i++)
        ib.put(ib.get(i - 1));
    fc.close();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel in = new FileInputStream("source.txt").getChannel(),
            out = new FileOutputStream("target.txt").getChannel();
    in.transferTo(0, in.size(), out);
    // Or:/*from w ww .j ava 2  s.  c  o m*/
    // out.transferFrom(in, 0, in.size());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    buf.force();//from  www  .  jav a 2s. c om

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    // Create a read-only memory-mapped file
    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();

    ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());
}

From source file:WordCount.java

public static void main(String args[]) throws Exception {
    String filename = "WordCount.java";

    // Map File from filename to byte buffer
    FileInputStream input = new FileInputStream(filename);
    FileChannel channel = input.getChannel();
    int fileLength = (int) channel.size();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength);

    // Convert to character buffer
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer charBuffer = decoder.decode(buffer);

    // Create line pattern
    Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE);

    // Create word pattern
    Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]");

    // Match line pattern to buffer
    Matcher lineMatcher = linePattern.matcher(charBuffer);

    Map map = new TreeMap();
    Integer ONE = new Integer(1);

    // For each line
    while (lineMatcher.find()) {
        // Get line
        CharSequence line = lineMatcher.group();

        // Get array of words on line
        String words[] = wordBreakPattern.split(line);

        // For each word
        for (int i = 0, n = words.length; i < n; i++) {
            if (words[i].length() > 0) {
                Integer frequency = (Integer) map.get(words[i]);
                if (frequency == null) {
                    frequency = ONE;/*from w ww .  j  av  a 2 s .c o  m*/
                } else {
                    int value = frequency.intValue();
                    frequency = new Integer(value + 1);
                }
                map.put(words[i], frequency);
            }
        }
    }
    System.out.println(map);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();/*from w  w w.j  a  va 2s . c om*/

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();//  w w  w  .  ja v a  2 s .co  m

    MappedByteBuffer mbb = buf.load();

    channel.close();
}