Java Scanner Read readFile(File file)

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

Description

Read a file and convert to String

License

Apache License

Parameter

Parameter Description
File a parameter

Exception

Parameter Description
IOException an exception

Return

String of the content of the file

Declaration

public static String readFile(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;

import java.util.Scanner;

public class Main {
    /**//w  ww  . ja v a  2s  . co  m
     * Read a file and convert to String
     * 
     * @param File
     * @return String of the content of the file
     * @throws IOException
     */
    public static String readFile(File file) throws IOException {
        StringBuilder result = new StringBuilder("");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            result.append(line).append("\n");
        }
        scanner.close();
        return result.toString();

    }
}

Related

  1. readFile(File f)
  2. readFile(File f)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File input)
  9. readFile(File path)