Java File to String fileToString(File file, int max)

Here you can find the source of fileToString(File file, int max)

Description

file To String

License

Open Source License

Declaration

public static String fileToString(File file, int max) 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 String fileToString(File file, int max) throws IOException {
        if (!file.exists() && file.canRead()) {
            throw new IllegalStateException("File " + file.getName() + " either does not exist or can not be read");
        }/*from  www.j  a va  2  s  .  c om*/
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        if (raf.length() > max) {
            raf.close();
            throw new IllegalStateException("File " + file.getName() + " is to large (>" + max);
        }
        byte[] ba = new byte[(int) raf.length()];
        raf.read(ba);
        raf.close();
        return new String(ba);
    }
}

Related

  1. fileToString(File file)
  2. fileToString(File file)
  3. fileToString(File file)
  4. fileToString(File file)
  5. fileToString(File file)
  6. fileToString(File file, String charsetName)
  7. fileToString(File file, String charsetName)
  8. fileToString(File files[])
  9. fileToString(final File file)