Java Scanner Read readFile(File f)

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

Description

This method reads the contents of a text file.

License

Open Source License

Parameter

Parameter Description
f The file to read from

Return

the contents of the text file as a single string

Declaration

public static String readFile(File f) 

Method Source Code

//package com.java2s;
/**//from   ww  w  .  j  a  va  2s  .  c  o  m
 * <p>A utility class for writing to, and reading from, text files.</p>
 *
 * <p>This program is part of AJP-P4-2012-2013-SOLUTION.</p>
 *
 * <p>AJP-P4-2012-2013-SOLUTION is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.</p>
 *
 * <p>This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.</p>
 *
 * <p>You should have received a copy of the GNU General Public License along
 * with this program. If not, see http://www.gnu.org/licenses/.</p>
 *
 * <p>Copyright Mark Truran m.a.truran@tees.ac.uk 27-Oct-2012 </p>
 */

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

import java.util.Scanner;

public class Main {
    /** This method reads the contents of a text file.
     * 
     * @param f The file to read from
     * @return the contents of the text file as a single string
     */
    public static String readFile(File f) {
        //Z means: "The end of the input but for the final terminator, if any
        String output = "No final terminator";
        try {
            output = new Scanner(f).useDelimiter("\\Z").next();
        } catch (FileNotFoundException e) {
            System.err.println("Problem reading from file");
        }
        return output;
    }
}

Related

  1. readArr(Scanner in, int n)
  2. readCQLFile(String cqlFileName)
  3. readFile(Class ownerClass, String fileName)
  4. readFile(File f)
  5. readFile(File f)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)