01: /*
02: ** $Id: JarFile.java,v 1.4 2000/10/26 08:34:15 mrw Exp $
03: **
04: ** Mike Wilson, October 2000, mrw@whisperingwind.co.uk
05: **
06: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
07: **
08: ** This program is free software; you can redistribute it and/or modify
09: ** it under the terms of the GNU General Public License as published by
10: ** the Free Software Foundation; either version 2 of the License, or
11: ** (at your option) any later version.
12: **
13: ** This program is distributed in the hope that it will be useful,
14: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: ** GNU General Public License for more details.
17: **
18: ** You should have received a copy of the GNU Library General
19: ** Public License along with this library; if not, write to the
20: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21: ** Boston, MA 02111-1307 USA.
22: */
23:
24: package uk.co.whisperingwind.framework;
25:
26: import java.io.BufferedReader;
27: import java.io.File;
28: import java.io.FileWriter;
29: import java.io.InputStream;
30: import java.io.InputStream;
31: import java.io.InputStreamReader;
32: import java.io.IOException;
33: import java.io.PrintWriter;
34: import java.net.URL;
35:
36: /**
37: ** Methods to access files contained in the same Jar file I am using.
38: */
39:
40: public class JarFile {
41: /**
42: ** Open an input stream from a file in my .JAR.
43: **
44: ** @param fileName the name of the file to open.
45: ** @return an InputStream for the selected file.
46: ** @exception IOException if the file cannot be opened.
47: */
48:
49: public static InputStream openFileFromJar(String fileName)
50: throws IOException {
51: String path = "/uk/co/whisperingwind/docs/" + fileName;
52: URL url = path.getClass().getResource(path);
53:
54: return url.openStream();
55: }
56:
57: /**
58: ** Copy a file, a line at a time, from the .JAR file to an output
59: ** file. By using readLine and println, I can be sure the target
60: ** file will have the correct text file format for the platform I
61: ** am running on.
62: **
63: ** @param fileName the name of the input file (in the .JAR).
64: ** @param target the target File -- the file to which the output
65: ** will be written.
66: ** @exception IOException if the input file cannot be opened or the
67: ** output file cannot be written.
68: */
69:
70: public static void copyTextFromJar(String fileName, File target)
71: throws IOException {
72: InputStream is = openFileFromJar(fileName);
73: InputStreamReader isr = new InputStreamReader(is);
74: BufferedReader input = new BufferedReader(isr);
75:
76: FileWriter writer = new FileWriter(target);
77: PrintWriter output = new PrintWriter(writer);
78:
79: String line = input.readLine();
80:
81: while (line != null) {
82: output.println(line);
83: line = input.readLine();
84: }
85:
86: input.close();
87: output.close();
88: }
89: }
|