Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / vividsolutions / jump / util / FileUtil.java @ 312

History | View | Annotate | Download (3.75 KB)

1

    
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
import java.io.*;
37
import java.util.*;
38

    
39

    
40
/**
41
 * File-related utility functions.
42
 */
43
public class FileUtil {
44
    /**
45
     * Reads a text file.
46
     * @param textFileName the pathname of the file to open
47
     * @return the lines of the text file
48
     * @throws FileNotFoundException if the text file is not found
49
     * @throws IOException if the file is not found or another I/O error occurs
50
     */
51
    public static List getContents(String textFileName)
52
        throws FileNotFoundException, IOException {
53
        List contents = new ArrayList();
54
        FileReader fileReader = new FileReader(textFileName);
55
        BufferedReader bufferedReader = new BufferedReader(fileReader);
56
        String line = bufferedReader.readLine();
57

    
58
        while (line != null) {
59
            contents.add(line);
60
            line = bufferedReader.readLine();
61
        }
62

    
63
        return contents;
64
    }
65

    
66
    /**
67
     * Saves the String to a file with the given filename.
68
     * @param textFileName the pathname of the file to create (or overwrite)
69
     * @param contents the data to save
70
     * @throws IOException if an I/O error occurs.
71
     */
72
    public static void setContents(String textFileName, String contents)
73
        throws IOException {
74
        FileWriter fileWriter = new FileWriter(textFileName, false);
75
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
76
        bufferedWriter.write(contents);
77
        bufferedWriter.flush();
78
        bufferedWriter.close();
79
        fileWriter.close();
80
    }
81

    
82
        public static List getContents(InputStream inputStream) throws IOException {
83
                ArrayList contents = new ArrayList();
84
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
85
                try {
86
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
87
                        try {
88
                                String line = bufferedReader.readLine();
89
                                while (line != null) {
90
                                        contents.add(line);
91
                                        line = bufferedReader.readLine();
92
                                }
93
                        } finally {
94
                                bufferedReader.close();
95
                        }
96
                } finally {
97
                        inputStreamReader.close();
98
                }
99
                return contents;
100
        }
101

    
102

    
103
    /**
104
     * Saves the List of Strings to a file with the given filename.
105
     * @param textFileName the pathname of the file to create (or overwrite)
106
     * @param lines the Strings to save as lines in the file
107
     * @throws IOException if an I/O error occurs.
108
     */
109
    public static void setContents(String textFileName, List lines)
110
        throws IOException {
111
        String contents = "";
112

    
113
        for (Iterator i = lines.iterator(); i.hasNext();) {
114
            String line = (String) i.next();
115
            contents += (line + System.getProperty("line.separator"));
116
        }
117

    
118
        setContents(textFileName, contents);
119
    }
120
}