001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.util;
016:
017: import java.io.*;
018: import java.util.*;
019:
020: /**
021: * Static functions for reading and writing text files as
022: * a single string, and treating a file as an ArrayList.
023: */
024: public class TextFile extends ArrayList {
025:
026: /**
027: * Read file as single string.
028: */
029: public static String read(String fileName) throws IOException {
030: File file = new File(fileName);
031: return read(file);
032: }
033:
034: /**
035: * Read file as single string.
036: */
037: public static String read(File fileName) throws IOException {
038: StringBuffer sb = new StringBuffer();
039: BufferedReader in = null;
040: try {
041: in = new BufferedReader(new FileReader(fileName));
042: String s;
043: while ((s = in.readLine()) != null) {
044: sb.append(s);
045: sb.append("\n");
046: }
047: } finally {
048: if (in != null)
049: in.close();
050: }
051: return sb.toString();
052: }
053:
054: /**
055: * Write file from a single string.
056: */
057: public static void write(String fileName, String text)
058: throws IOException {
059: File file = new File(fileName);
060: write(file, text);
061: }
062:
063: /**
064: * Write file from a single string.
065: */
066: public static void write(File file, String text) throws IOException {
067: PrintWriter out = null;
068: try {
069: out = new PrintWriter(new BufferedWriter(new FileWriter(
070: file, false)));
071: out.print(text);
072: } finally {
073: if (out != null)
074: out.close();
075: }
076: }
077:
078: public TextFile(String fileName) throws IOException {
079: super (Arrays.asList(read(fileName).split("\n")));
080: }
081:
082: /**
083: * Write file from a single string.
084: */
085: public void write(String fileName) throws IOException {
086: PrintWriter out = null;
087: try {
088: out = new PrintWriter(new BufferedWriter(new FileWriter(
089: fileName)));
090: for (int i = 0; i < size(); i++)
091: out.println(get(i));
092: } finally {
093: if (out != null)
094: out.close();
095: }
096: }
097:
098: /**
099: * Read file as single string.
100: */
101: public static String read(String fileName, Object parent)
102: throws IOException {
103: StringBuffer sb = new StringBuffer();
104: InputStream is = null;
105: BufferedReader in = null;
106: try {
107: is = parent.getClass().getResourceAsStream(fileName);
108: in = new BufferedReader(new InputStreamReader(is));
109: String s;
110: while ((s = in.readLine()) != null) {
111: sb.append(s);
112: sb.append("\n");
113: }
114: } finally {
115: if (is != null)
116: is.close();
117: if (in != null)
118: in.close();
119: }
120: return sb.toString();
121: }
122: }
|