Java File to String fileToString(File file)

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

Description

Reads a complete text file.

License

Open Source License

Parameter

Parameter Description
file The text file to read.

Return

the file content.

Declaration

public static String fileToString(File file) 

Method Source Code

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

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Main {
    /**//from  ww  w . j  a  v a2 s .  c  om
     * Reads a complete text file.
     *
     * @param file The text file to read.
     * @return the file content.
     */
    public static String fileToString(File file) {
        try {
            return new Scanner(file).useDelimiter("\\Z").next();
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Failed to read file: " + file, e);
        }
    }
}

Related

  1. fileToString(File f, String encoding)
  2. fileToString(File file)
  3. fileToString(File file)
  4. fileToString(File file)
  5. fileToString(File file)
  6. fileToString(File file)
  7. fileToString(File file)
  8. fileToString(File file)
  9. fileToString(File file, int max)