001 /*
002 * LAPIS lightweight structured text processing system
003 *
004 * Copyright (C) 1998-2002 Carnegie Mellon University,
005 * Copyright (C) 2003 Massachusetts Institute of Technology.
006 * All rights reserved.
007 *
008 * This library is free software; you can redistribute it
009 * and/or modify it under the terms of the GNU General
010 * Public License as published by the Free Software
011 * Foundation, version 2.
012 *
013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014 */
015
016 package lapisx.io;
017
018 import java.io.*;
019 //import java.util.*;
020
021 public abstract class FileUtil {
022 /**
023 * Load a file into a string.
024 * @param f Filename to load
025 * @return Contents of file as a string, loaded through a Reader
026 */
027 public static String load (File f) throws IOException {
028 return load (new FileReader (f));
029 }
030
031 /**
032 * Load a stream into a string.
033 * @param in Stream to load
034 * @return Contents of stream as a string
035 */
036 public static String load (InputStream in) throws IOException {
037 return load (new InputStreamReader (in));
038 }
039
040 /**
041 * Load a reader stream into a string.
042 * @param r Reader stream to load
043 * @return Contents of reader stream as a string
044 */
045 public static String load (Reader r) throws IOException {
046 int n;
047 StringBuffer sbuf = new StringBuffer ();
048 char[] buf = new char[8000];
049 while ((n = r.read (buf)) != -1)
050 sbuf.append (buf, 0, n);
051 return sbuf.toString ();
052 }
053
054 /**
055 * Save a file (represented as a string) to disk.
056 * @param f Filename to save
057 * @param s Contents of file
058 */
059 public static void save (File f, String s) throws IOException {
060 PrintWriter out = new PrintWriter (new FileWriter (f));
061 out.print (s);
062 out.close ();
063 }
064 }