Java tutorial
//package com.java2s; /* * XML Sequences for mission critical IT procedures * * Copyright 2004-2010 Operational Dynamics Consulting, Pty Ltd * * The code in this file, and the program it is a part of, is made available * to you by its authors as open source software: you can redistribute it * and/or modify it under the terms of the GNU General Public License version * 2 ("GPL") as published by the Free Software Foundation. * * 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 GPL for more details. * * You should have received a copy of the GPL along with this program. If not, * see http://www.gnu.org/licenses/. The authors of this program may be * contacted through http://research.operationaldynamics.com/projects/xseq/. */ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Main { /** * Read the contents of a file into a single Java String. * * @param filename * The file to be read in * @return a String containing the contents of filename * @throws FileNotFoundException * and IOException on a read error. These are propegated * rather than simply trapped here bcause if reading in files * is somewhat critical to the app and if something goes * wrong, the app needs to take remedial action, probably with * user feedback. */ public static String fileToString(String filename) throws IOException, FileNotFoundException { BufferedReader in = new BufferedReader(new FileReader(filename)); StringBuffer sb = new StringBuffer(); String str = null; while ((str = in.readLine()) != null) { // oops, that looses newlines, so put 'em back in sb.append(str); sb.append('\n'); } in.close(); return sb.toString(); } }