Java Scanner Read readFile(String filePath)

Here you can find the source of readFile(String filePath)

Description

read File

License

Apache License

Declaration

public static String readFile(String filePath) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*from  w  ww  . j  a  v a 2s.c  o  m*/
 * Copyright (c) 2004-2016 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;

public class Main {
    public static String readFile(String filePath) throws FileNotFoundException {
        return readFile(new File(filePath));
    }

    public static String readFile(File file) throws FileNotFoundException {
        // \A == The beginning of the input
        Scanner scanner = new Scanner(file, "UTF-8");
        String result = scanner.useDelimiter("\\A").next();
        scanner.close();
        return result;
    }
}

Related

  1. readFile(String file_name)
  2. readFile(String filename)
  3. readFile(String fileName)
  4. readFile(String fileName)
  5. readFile(String fileName)
  6. readFile(String filePath)
  7. readFile(String filePath)
  8. readFile(String path)
  9. readFile(String pathname)