Statistics
| Revision:

svn-gvsig-desktop / trunk / prototypes / mobile / desktop / extensions / extExportMobile / src / es / prodevelop / gvsig / exportMobile / files / NewDir.java @ 19196

History | View | Annotate | Download (4.05 KB)

1
package es.prodevelop.gvsig.exportMobile.files;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7

    
8
import org.apache.log4j.Logger;
9

    
10

    
11
/**
12
 * Manages the directory when a gvsig mobile project 
13
 * with his data will be stored
14
 * 
15
 * @author Anabel Moreno
16
 *
17
 */
18
public class NewDir {
19

    
20
        private static Logger logger = Logger.getLogger(NewDir.class.getName());
21

    
22
        private String namePath;
23

    
24
        private File newFileProy;
25

    
26
        private File[] filesDir;
27

    
28
        private int numFiles;
29
        
30
        public static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami";
31

    
32
        /**
33
         * Create new output file
34
         * 
35
         * @param dirOutPut
36
         */
37
        public NewDir(String dirOutPut) {
38

    
39
                namePath = (dirOutPut + File.separator);
40
                newFileProy = new File(namePath);
41

    
42
        }
43

    
44
        /** 
45
         * @return true if the directory is empty
46
         */
47
        public boolean isEmpty() {
48

    
49
                filesDir = newFileProy.listFiles();
50
                numFiles = filesDir.length;
51

    
52
                if (contFiles(filesDir) == 0)
53
                        return true;
54
                else
55
                        return false;
56

    
57
        }
58

    
59
        /**
60
         * Count the name of files in a directory
61
         * 
62
         * @param arrFiles
63
         * @return the number of files in a directory
64
         */
65
        private int contFiles(File[] arrFiles) {
66

    
67
                int cont = 0;
68
                for (int i = 0; i < arrFiles.length; i++) {
69

    
70
                        if (arrFiles[i].exists()) {
71
                                cont++;
72
                        }
73
                }
74
                return cont;
75
        }
76

    
77
        /**
78
         * Delete files
79
         */
80
        public void deleteFiles() {
81

    
82
                for (int i = 0; i < numFiles; i++) {
83
                        filesDir[i].delete();
84

    
85
                }
86

    
87
        }
88

    
89
        /**
90
         * if file exists and is directory, while this file is full, the files that
91
         * are into the file will be deleted
92
         * 
93
         * @return output directory
94
         */
95
        public File ifExistDir() {
96

    
97
                File outputPath = null;
98

    
99
                if (newFileProy.exists()) {
100

    
101
                        if (newFileProy.isDirectory()) {
102

    
103
                                // to delete, the directory has to be empty
104

    
105
                                while (isEmpty() == false) {
106

    
107
                                        deleteFiles();
108
                                }
109

    
110
                                File deletedFile = newFileProy;
111
                                boolean delete = deletedFile.delete();
112
                                // if (delete) System.out.println("delete file");
113
                                // else System.out.println("not delete");
114

    
115
                        }
116
                }
117

    
118
                /** new directory */
119

    
120
                boolean mk = newFileProy.mkdir();
121

    
122
                if (mk == true) {
123

    
124
                        /* create output File for result layer */
125
                        outputPath = new File(newFileProy.getAbsolutePath());
126

    
127
                }
128

    
129
                return outputPath;
130
        }
131
        
132
        public static void copyFile(File in, File out) {
133

    
134
                FileInputStream fis = null;
135
                FileOutputStream fos = null;
136
                try {
137
                    fis  = new FileInputStream(in);
138
                    fos = new FileOutputStream(out);
139
                byte[] buf = new byte[1024];
140
                int i = 0;
141
                while ((i = fis.read(buf)) != -1) {
142
                    fos.write(buf, 0, i);
143
                }
144
                if (fis != null) fis.close();
145
                if (fos != null) fos.close();
146
            } catch (Exception e) {
147
                logger.error("Unable to copy file: " + e.getMessage());
148
            }
149
        }
150
        
151
        public static void copyShpDbfShxTempToFinal(File source, File dest) {
152
                
153
                if (dest == null) {
154
                        logger.error("Unable to copy temp to file: finalFile is NULL !");
155
                }
156
                
157
                if (source == null) {
158
                        logger.error("Unable to copy temp to file: tempFile is NULL !");
159
                }
160
                
161
                int temp_last_shp = source.getAbsolutePath().toLowerCase().lastIndexOf(".shp");
162
                String temp_base = source.getAbsolutePath().substring(0, temp_last_shp);
163
                int final_last_shp = dest.getAbsolutePath().toLowerCase().lastIndexOf(".shp");
164
                String final_base = dest.getAbsolutePath().substring(0, final_last_shp);
165
                
166
                NewDir.copyFile(source, dest);
167
                
168
                File f_in = new File(temp_base + ".dbf"); 
169
                File f_out = new File(final_base + ".dbf");
170
                NewDir.copyFile(f_in, f_out);
171
                
172
                f_in = new File(temp_base + ".shx"); 
173
                f_out = new File(final_base + ".shx");
174
                NewDir.copyFile(f_in, f_out);
175
        }
176

    
177
        public static File getTempShpFile() {
178

    
179
                File f = null;
180
                String t = "" + System.currentTimeMillis();
181
                try {
182
                        f = File.createTempFile(t, ".shp");
183
                        f.deleteOnExit();
184
                } catch (IOException e) {
185
                        logger.error("Unable to create temp file: " + e.getMessage());
186
                }
187
                return f;
188
        }
189
        
190
}