Example usage for java.io FileInputStream getChannel

List of usage examples for java.io FileInputStream getChannel

Introduction

In this page you can find the example usage for java.io FileInputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file input stream.

Usage

From source file:ExplicitChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;
    FileChannel fChan;/*from w  ww . j av  a  2  s. c  o m*/
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = ByteBuffer.allocate((int) fSize);
        fChan.read(mBuf);
        mBuf.rewind();
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MappedChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;
    FileChannel fChan;/*from  ww w .  j  ava 2s  . c o  m*/
    long fSize;
    MappedByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());

        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileInputStream fIn;
    FileChannel fChan;/*w  w w . j  a  v a  2 s. co  m*/
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");

        fChan = fIn.getChannel();

        fSize = fChan.size();

        mBuf = ByteBuffer.allocate((int) fSize);

        fChan.read(mBuf);

        mBuf.rewind();

        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    int i = 0;/*w  w  w  .j  av  a 2  s  . co  m*/

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // get file channel
        FileChannel fc = fis.getChannel();

        // get channel position
        long pos = fc.position();

        char c = (char) i;

        System.out.println("No of bytes read: " + pos);
        System.out.println("Char read: " + c);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileInputStream fileInputStream;
    FileChannel fileChannel;//from  w  w  w.  j av  a 2 s.c o  m
    long fileSize;
    MappedByteBuffer mBuf;

    try {
        fileInputStream = new FileInputStream("test.txt");
        fileChannel = fileInputStream.getChannel();
        fileSize = fileChannel.size();
        mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);

        for (int i = 0; i < fileSize; i++)
            System.out.print((char) mBuf.get());

        fileChannel.close();
        fileInputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String[] a) {
    File aFile = new File("charData.txt");
    FileInputStream inFile = null;
    try {//from w w w .jav  a  2 s . c  o m
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileChannel inChannel = inFile.getChannel();
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("C:/test.bin");
    FileInputStream inFile = null;
    try {//from  ww w.ja  v a 2  s .  c o  m
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    final int PRIMECOUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT);
    long[] primes = new long[PRIMECOUNT];
    try {
        while (inChannel.read(buf) != -1) {
            ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes);
            for (long prime : primes) {
                System.out.printf("%10d", prime);
            }
            buf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:BGrep.java

public static void main(String[] args) {
    String encodingName = "UTF-8"; // Default to UTF-8 encoding
    int flags = Pattern.MULTILINE; // Default regexp flags

    try { // Fatal exceptions are handled after this try block
        // First, process any options
        int nextarg = 0;
        while (args[nextarg].charAt(0) == '-') {
            String option = args[nextarg++];
            if (option.equals("-e")) {
                encodingName = args[nextarg++];
            } else if (option.equals("-i")) { // case-insensitive matching
                flags |= Pattern.CASE_INSENSITIVE;
            } else if (option.equals("-s")) { // Strict Unicode processing
                flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode
                flags |= Pattern.CANON_EQ; // canonicalize Unicode
            } else {
                System.err.println("Unknown option: " + option);
                usage();/*w  w  w  . jav  a2 s .  co m*/
            }
        }

        // Get the Charset for converting bytes to chars
        Charset charset = Charset.forName(encodingName);

        // Next argument must be a regexp. Compile it to a Pattern object
        Pattern pattern = Pattern.compile(args[nextarg++], flags);

        // Require that at least one file is specified
        if (nextarg == args.length)
            usage();

        // Loop through each of the specified filenames
        while (nextarg < args.length) {
            String filename = args[nextarg++];
            CharBuffer chars; // This will hold complete text of the file
            try { // Handle per-file errors locally
                // Open a FileChannel to the named file
                FileInputStream stream = new FileInputStream(filename);
                FileChannel f = stream.getChannel();

                // Memory-map the file into one big ByteBuffer. This is
                // easy but may be somewhat inefficient for short files.
                ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size());

                // We can close the file once it is is mapped into memory.
                // Closing the stream closes the channel, too.
                stream.close();

                // Decode the entire ByteBuffer into one big CharBuffer
                chars = charset.decode(bytes);
            } catch (IOException e) { // File not found or other problem
                System.err.println(e); // Print error message
                continue; // and move on to the next file
            }

            // This is the basic regexp loop for finding all matches in a
            // CharSequence. Note that CharBuffer implements CharSequence.
            // A Matcher holds state for a given Pattern and text.
            Matcher matcher = pattern.matcher(chars);
            while (matcher.find()) { // While there are more matches
                // Print out details of the match
                System.out.println(filename + ":" + // file name
                        matcher.start() + ": " + // character pos
                        matcher.group()); // matching text
            }
        }
    }
    // These are the things that can go wrong in the code above
    catch (UnsupportedCharsetException e) { // Bad encoding name
        System.err.println("Unknown encoding: " + encodingName);
    } catch (PatternSyntaxException e) { // Bad pattern
        System.err.println("Syntax error in search pattern:\n" + e.getMessage());
    } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments
        usage();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("primes.txt");
    FileInputStream inFile = null;
    try {//from w ww  . ja va  2  s.  c  o  m
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    try {
        ByteBuffer lengthBuf = ByteBuffer.allocate(8);
        while (true) {
            if (inChannel.read(lengthBuf) == -1) {
                break;
            }
            lengthBuf.flip();
            int strLength = (int) lengthBuf.getDouble();
            ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
            if (inChannel.read(buf) == -1) {
                break;
            }
            buf.flip();
            byte[] strChars = new byte[2 * strLength];
            buf.get(strChars);
            System.out.println(strLength);
            System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
            System.out.println(buf.getLong());
            lengthBuf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:NIOCopy.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn;
    FileOutputStream fOut;/*from   w w  w  .j a  va  2s . c  o m*/
    FileChannel fIChan, fOChan;
    long fSize;
    MappedByteBuffer mBuf;

    fIn = new FileInputStream(args[0]);
    fOut = new FileOutputStream(args[1]);

    fIChan = fIn.getChannel();
    fOChan = fOut.getChannel();
    fSize = fIChan.size();

    mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
    fOChan.write(mBuf); // this copies the file
    fIChan.close();
    fIn.close();

    fOChan.close();
    fOut.close();
}