Java DataInputStream Create getReaderFor(File file)

Here you can find the source of getReaderFor(File file)

Description

get Reader For

License

Open Source License

Declaration

public static DataInputStream getReaderFor(File file)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static DataInputStream getReaderFor(File file)
            throws IOException {

        DataInputStream data;//from   w  w w .j a v  a  2 s  .co m

        // If the file is longer than we can fit into an array, use the slow method.
        if (file.length() > Integer.MAX_VALUE)
            data = new DataInputStream(new BufferedInputStream(
                    new FileInputStream(file)));
        // Otherwise, read from a byte array, which is 4x faster on my machine.
        else {

            InputStream stream = new BufferedInputStream(
                    new FileInputStream(file), 256);
            byte[] bytes = new byte[(int) file.length()];
            stream.read(bytes, 0, bytes.length);
            stream.close();
            data = new DataInputStream(new ByteArrayInputStream(bytes));

        }

        return data;

    }
}

Related

  1. getInputStream(String file)
  2. getInputStream(String path)