Java Text File Read All readAll(File file)

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

Description

read All

License

Open Source License

Declaration

public static String readAll(File file) 

Method Source Code


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

import java.io.*;

public class Main {

    public static String readAll(File file) {
        try (InputStream in = new FileInputStream(file)) {
            byte[] bs = new byte[(int) file.length()];
            int p = 0, len;
            while ((len = in.read(bs, p, bs.length - p)) >= 0)
                p += len;//from w  w  w. j  a  v a  2 s  .  co  m
            if (p != bs.length)
                throw new RuntimeException();
            return new String(bs);
        } catch (IOException e) {
            return null;
        }
    }

    public static String read(String file) {
        return readAll(new File(file));
    }
}

Related

  1. readAll(File file)
  2. readAll(final String path)
  3. readAllFile(File file)
  4. readAllFile(String fileName)