Here you can find the source of readFile(String filename)
Parameter | Description |
---|---|
filename | The filename to read from. |
Parameter | Description |
---|---|
IOException | Thrown when an I/O error occurs. |
public static String readFile(String filename) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2004, 2006 The Board of Trustees of Stanford University. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU General Public License * which is available at http://www.gnu.org/licenses/gpl.txt. *******************************************************************************/ import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { /**/*from w w w .jav a 2s .c om*/ * Read a file's contents into a string. * * (copied from csli.agent.manager.DisplayFile) * * @param filename * The filename to read from. * @return The contents of <i>filename</i>. * * @throws IOException * Thrown when an I/O error occurs. */ public static String readFile(String filename) throws IOException { File file = new File(filename); StringBuffer buf = new StringBuffer((int) file.length() + 2); FileReader in = new FileReader(file); int c; while ((c = in.read()) != -1) { buf.append((char) c); } in.close(); return (buf.toString()); } }