Java Scanner Read readFile(File input)

Here you can find the source of readFile(File input)

Description

Reads a file and outputs it as String

License

Open Source License

Parameter

Parameter Description
input text file

Exception

Parameter Description
FileNotFoundException an exception

Return

String document

Declaration

public static String readFile(File input) throws FileNotFoundException 

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 www.ja  va  2 s.  c o m
     * Reads a file and outputs it as String
     * @param input text file
     * @return String document
     * @throws FileNotFoundException
     */
    public static String readFile(File input) throws FileNotFoundException {
        StringBuilder fileBuffer = new StringBuilder((int) input.length());
        Scanner scanner = new Scanner(input);
        try {
            while (scanner.hasNextLine()) {
                fileBuffer.append(scanner.nextLine()).append('\n');
            }
        } finally {
            scanner.close();
        }
        return fileBuffer.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File path)
  7. readFile(final File f)
  8. readFile(String file_name)
  9. readFile(String filename)