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) 

Method Source Code


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

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

import java.util.Scanner;

public class Main {
    public static String readTextFile(File file) {
        try {/*  w  w w  .j  av  a  2s . co  m*/
            return readText(new FileInputStream(file));
        } catch (Exception e) {
            throw new RuntimeException("Reading text file \"" + file + "\" failed", e);
        }
    }

    public static String readText(InputStream in) {
        StringBuilder text = new StringBuilder();
        Scanner scanner = new Scanner(in);
        while (scanner.hasNextLine()) {
            text.append(scanner.nextLine() + "\n");
        }
        return text.toString();
    }
}

Related

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