Java Scanner Read All readTextFile(File file)

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

Description

read Text File

License

Open Source License

Declaration

public static String readTextFile(File file) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Gabriel Skantze./*www  .  j  a va 2 s . c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Gabriel Skantze - initial API and implementation
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String readTextFile(File file) throws IOException {
        /*
        byte[] buffer = new byte[(int) file.length()];
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
        is.read(buffer);
        is.close();
        return new String(buffer);
         */
        return readString(new FileInputStream(file));
    }

    public static String readString(InputStream in) throws IOException {
        //return IOUtils.toString(in, "UTF-8");
        java.util.Scanner s = new java.util.Scanner(in, "UTF-8")
                .useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
}

Related

  1. readTextFile(File file)
  2. readTextFile(String filename)
  3. readTextFile(String path)
  4. readTextFile(String path)