svn-gvsig-desktop / tags / v1_0_2_RELEASE / applications / appgvSIG / src / com / vividsolutions / jump / util / FileUtil.java @ 11432
History | View | Annotate | Download (4.02 KB)
1 | 312 | fernando | |
---|---|---|---|
2 | /*
|
||
3 | * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
||
4 | * for visualizing and manipulating spatial features with geometry and attributes.
|
||
5 | *
|
||
6 | * Copyright (C) 2003 Vivid Solutions
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (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 General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Vivid Solutions
|
||
25 | * Suite #1A
|
||
26 | * 2328 Government Street
|
||
27 | * Victoria BC V8T 5G5
|
||
28 | * Canada
|
||
29 | *
|
||
30 | * (250)385-6040
|
||
31 | * www.vividsolutions.com
|
||
32 | */
|
||
33 | |||
34 | package com.vividsolutions.jump.util; |
||
35 | |||
36 | 8765 | jjdelcerro | import java.io.BufferedReader; |
37 | import java.io.BufferedWriter; |
||
38 | import java.io.FileNotFoundException; |
||
39 | import java.io.FileReader; |
||
40 | import java.io.FileWriter; |
||
41 | import java.io.IOException; |
||
42 | import java.io.InputStream; |
||
43 | import java.io.InputStreamReader; |
||
44 | import java.util.ArrayList; |
||
45 | import java.util.Iterator; |
||
46 | import java.util.List; |
||
47 | 312 | fernando | |
48 | |||
49 | /**
|
||
50 | * File-related utility functions.
|
||
51 | */
|
||
52 | public class FileUtil { |
||
53 | /**
|
||
54 | * Reads a text file.
|
||
55 | * @param textFileName the pathname of the file to open
|
||
56 | * @return the lines of the text file
|
||
57 | * @throws FileNotFoundException if the text file is not found
|
||
58 | * @throws IOException if the file is not found or another I/O error occurs
|
||
59 | */
|
||
60 | public static List getContents(String textFileName) |
||
61 | throws FileNotFoundException, IOException { |
||
62 | List contents = new ArrayList(); |
||
63 | FileReader fileReader = new FileReader(textFileName); |
||
64 | BufferedReader bufferedReader = new BufferedReader(fileReader); |
||
65 | String line = bufferedReader.readLine();
|
||
66 | |||
67 | while (line != null) { |
||
68 | contents.add(line); |
||
69 | line = bufferedReader.readLine(); |
||
70 | } |
||
71 | |||
72 | return contents;
|
||
73 | } |
||
74 | |||
75 | /**
|
||
76 | * Saves the String to a file with the given filename.
|
||
77 | * @param textFileName the pathname of the file to create (or overwrite)
|
||
78 | * @param contents the data to save
|
||
79 | * @throws IOException if an I/O error occurs.
|
||
80 | */
|
||
81 | public static void setContents(String textFileName, String contents) |
||
82 | throws IOException { |
||
83 | FileWriter fileWriter = new FileWriter(textFileName, false); |
||
84 | BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); |
||
85 | bufferedWriter.write(contents); |
||
86 | bufferedWriter.flush(); |
||
87 | bufferedWriter.close(); |
||
88 | fileWriter.close(); |
||
89 | } |
||
90 | |||
91 | public static List getContents(InputStream inputStream) throws IOException { |
||
92 | ArrayList contents = new ArrayList(); |
||
93 | InputStreamReader inputStreamReader = new InputStreamReader(inputStream); |
||
94 | try {
|
||
95 | BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
||
96 | try {
|
||
97 | String line = bufferedReader.readLine();
|
||
98 | while (line != null) { |
||
99 | contents.add(line); |
||
100 | line = bufferedReader.readLine(); |
||
101 | } |
||
102 | } finally {
|
||
103 | bufferedReader.close(); |
||
104 | } |
||
105 | } finally {
|
||
106 | inputStreamReader.close(); |
||
107 | } |
||
108 | return contents;
|
||
109 | } |
||
110 | |||
111 | |||
112 | /**
|
||
113 | * Saves the List of Strings to a file with the given filename.
|
||
114 | * @param textFileName the pathname of the file to create (or overwrite)
|
||
115 | * @param lines the Strings to save as lines in the file
|
||
116 | * @throws IOException if an I/O error occurs.
|
||
117 | */
|
||
118 | public static void setContents(String textFileName, List lines) |
||
119 | throws IOException { |
||
120 | String contents = ""; |
||
121 | |||
122 | for (Iterator i = lines.iterator(); i.hasNext();) { |
||
123 | String line = (String) i.next(); |
||
124 | contents += (line + System.getProperty("line.separator")); |
||
125 | } |
||
126 | |||
127 | setContents(textFileName, contents); |
||
128 | } |
||
129 | } |