Here you can find the source of readFile(File f)
Parameter | Description |
---|---|
f | The file to read from |
public static String readFile(File f)
//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; } }