1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.item.util; |
18 | |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | |
22 | import org.springframework.batch.item.ItemStreamException; |
23 | import org.springframework.util.Assert; |
24 | |
25 | /** |
26 | * Utility methods for files used in batch processing. |
27 | * |
28 | * @author Peter Zozom |
29 | */ |
30 | public final class FileUtils { |
31 | |
32 | // forbids instantiation |
33 | private FileUtils() { |
34 | } |
35 | |
36 | /** |
37 | * Set up output file for batch processing. This method implements common logic for handling output files when |
38 | * starting or restarting file I/O. When starting output file processing, creates/overwrites new file. When |
39 | * restarting output file processing, checks whether file is writable. |
40 | * |
41 | * @param file file to be set up |
42 | * @param restarted true signals that we are restarting output file processing |
43 | * @param append true signals input file may already exist (but doesn't have to) |
44 | * @param overwriteOutputFile If set to true, output file will be overwritten (this flag is ignored when processing |
45 | * is restart) |
46 | * |
47 | * @throws IllegalArgumentException when file is null |
48 | * @throws ItemStreamException when starting output file processing, file exists and flag "overwriteOutputFile" is |
49 | * set to false |
50 | * @throws ItemStreamException when unable to create file or file is not writable |
51 | */ |
52 | public static void setUpOutputFile(File file, boolean restarted, boolean append, boolean overwriteOutputFile) { |
53 | |
54 | Assert.notNull(file); |
55 | |
56 | try { |
57 | if (!restarted) { |
58 | if (!append) { |
59 | if (file.exists()) { |
60 | if (!overwriteOutputFile) { |
61 | throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]"); |
62 | } |
63 | if (!file.delete()) { |
64 | throw new IOException("Could not delete file: " + file); |
65 | } |
66 | } |
67 | |
68 | if (file.getParent() != null) { |
69 | new File(file.getParent()).mkdirs(); |
70 | } |
71 | if (!createNewFile(file)) { |
72 | throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() + "]"); |
73 | } |
74 | } |
75 | else { |
76 | if (!file.exists()) { |
77 | if (file.getParent() != null) { |
78 | new File(file.getParent()).mkdirs(); |
79 | } |
80 | if (!createNewFile(file)) { |
81 | throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() |
82 | + "]"); |
83 | } |
84 | } |
85 | } |
86 | } |
87 | } |
88 | catch (IOException ioe) { |
89 | throw new ItemStreamException("Unable to create file: [" + file.getAbsolutePath() + "]", ioe); |
90 | } |
91 | |
92 | if (!file.canWrite()) { |
93 | throw new ItemStreamException("File is not writable: [" + file.getAbsolutePath() + "]"); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * @deprecated use the version with explicit append parameter instead. Here append=false is assumed. |
99 | */ |
100 | public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) { |
101 | setUpOutputFile(file, restarted, false, overwriteOutputFile); |
102 | } |
103 | |
104 | /** |
105 | * Create a new file if it doesn't already exist. |
106 | * |
107 | * @param file the file to create on the filesystem |
108 | */ |
109 | public static boolean createNewFile(File file) throws IOException { |
110 | |
111 | if (file.exists()) { |
112 | return false; |
113 | } |
114 | |
115 | try { |
116 | return file.createNewFile() && file.exists(); |
117 | } |
118 | catch (IOException e) { |
119 | // On some filesystems you can get an exception here even though the |
120 | // files was successfully created |
121 | if (file.exists()) { |
122 | return true; |
123 | } |
124 | else { |
125 | throw e; |
126 | } |
127 | } |
128 | |
129 | } |
130 | |
131 | } |