Java RandomAccessFile Read readFile(String file)

Here you can find the source of readFile(String file)

Description

read File

License

Open Source License

Declaration

public static byte[] readFile(String file) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Main {
    public static byte[] readFile(String file) throws IOException {
        return readFile(new File(file));
    }//from w  w w.  j a v a  2 s  .  co m

    public static byte[] readFile(File file) throws IOException {
        // Open file
        RandomAccessFile f = new RandomAccessFile(file, "r");
        try {
            // Get and check length
            long longlength = f.length();
            int length = (int) longlength;
            if (length != longlength)
                throw new IOException("File size >= 2 GB");
            // Read file and return data
            byte[] data = new byte[length];
            f.readFully(data);
            return data;
        } finally {
            f.close();
        }
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(final File file)
  4. readFile(String absoluteFileName)
  5. readFile(String file)
  6. readFile(String filename)
  7. readFile(String filePath)
  8. readFileByRandomAccess(String fileName)
  9. readFileBytes(final File file)