001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI for
003: * visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
019: * Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions Suite #1A 2328 Government Street Victoria BC V8T 5G5 Canada
024: *
025: * (250)385-6040 www.vividsolutions.com
026: */
027:
028: package com.vividsolutions.jump.util;
029:
030: import java.io.BufferedInputStream;
031: import java.io.BufferedOutputStream;
032: import java.io.BufferedReader;
033: import java.io.BufferedWriter;
034: import java.io.File;
035: import java.io.FileInputStream;
036: import java.io.FileNotFoundException;
037: import java.io.FileOutputStream;
038: import java.io.FileReader;
039: import java.io.FileWriter;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.io.InputStreamReader;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.zip.ZipEntry;
048: import java.util.zip.ZipOutputStream;
049:
050: /**
051: * File-related utility functions.
052: */
053: public class FileUtil {
054: /**
055: * Reads a text file.
056: *
057: * @param textFileName
058: * the pathname of the file to open
059: * @return the lines of the text file
060: * @throws FileNotFoundException
061: * if the text file is not found
062: * @throws IOException
063: * if the file is not found or another I/O error occurs
064: */
065: public static List getContents(String textFileName)
066: throws FileNotFoundException, IOException {
067: List contents = new ArrayList();
068: FileReader fileReader = new FileReader(textFileName);
069: BufferedReader bufferedReader = new BufferedReader(fileReader);
070: String line = bufferedReader.readLine();
071:
072: while (line != null) {
073: contents.add(line);
074: line = bufferedReader.readLine();
075: }
076:
077: return contents;
078: }
079:
080: /**
081: * Saves the String to a file with the given filename.
082: *
083: * @param textFileName
084: * the pathname of the file to create (or overwrite)
085: * @param contents
086: * the data to save
087: * @throws IOException
088: * if an I/O error occurs.
089: */
090: public static void setContents(String textFileName, String contents)
091: throws IOException {
092: FileWriter fileWriter = new FileWriter(textFileName, false);
093: BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
094: bufferedWriter.write(contents);
095: bufferedWriter.flush();
096: bufferedWriter.close();
097: fileWriter.close();
098: }
099:
100: public static List getContents(InputStream inputStream)
101: throws IOException {
102: ArrayList contents = new ArrayList();
103: InputStreamReader inputStreamReader = new InputStreamReader(
104: inputStream);
105: try {
106: BufferedReader bufferedReader = new BufferedReader(
107: inputStreamReader);
108: try {
109: String line = bufferedReader.readLine();
110: while (line != null) {
111: contents.add(line);
112: line = bufferedReader.readLine();
113: }
114: } finally {
115: bufferedReader.close();
116: }
117: } finally {
118: inputStreamReader.close();
119: }
120: return contents;
121: }
122:
123: /**
124: * Saves the List of Strings to a file with the given filename.
125: *
126: * @param textFileName
127: * the pathname of the file to create (or overwrite)
128: * @param lines
129: * the Strings to save as lines in the file
130: * @throws IOException
131: * if an I/O error occurs.
132: */
133: public static void setContents(String textFileName, List lines)
134: throws IOException {
135: String contents = "";
136:
137: for (Iterator i = lines.iterator(); i.hasNext();) {
138: String line = (String) i.next();
139: contents += (line + System.getProperty("line.separator"));
140: }
141:
142: setContents(textFileName, contents);
143: }
144:
145: public static void zip(Collection files, File zipFile)
146: throws IOException {
147: FileOutputStream fos = new FileOutputStream(zipFile);
148: try {
149: BufferedOutputStream bos = new BufferedOutputStream(fos);
150: try {
151: ZipOutputStream zos = new ZipOutputStream(bos);
152: try {
153: for (Iterator i = files.iterator(); i.hasNext();) {
154: File file = (File) i.next();
155: zos.putNextEntry(new ZipEntry(file.getName()));
156: FileInputStream fis = new FileInputStream(file);
157: try {
158: BufferedInputStream bis = new BufferedInputStream(
159: fis);
160: try {
161: while (true) {
162: int j = bis.read();
163: if (j == -1) {
164: break;
165: }
166: zos.write(j);
167: }
168: } finally {
169: bis.close();
170: }
171: } finally {
172: fis.close();
173: zos.closeEntry();
174: }
175: }
176: } finally {
177: zos.close();
178: }
179: } finally {
180: bos.close();
181: }
182: } finally {
183: fos.close();
184: }
185: }
186:
187: public static String getExtension(File f) {
188: String ext = "";
189: String s = f.getName();
190: int i = s.lastIndexOf('.');
191:
192: if ((i > 0) && (i < (s.length() - 1))) {
193: ext = s.substring(i + 1).toLowerCase();
194: }
195:
196: return ext;
197: }
198:
199: public static File addExtensionIfNone(File file, String extension) {
200: if (getExtension(file).length() > 0) {
201: return file;
202: }
203: String path = file.getAbsolutePath();
204: if (!path.endsWith(".")) {
205: path += ".";
206: }
207: path += extension;
208: return new File(path);
209: }
210:
211: }
|