View Javadoc

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 (!createNewFile(file)) {
78  							throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath()
79  									+ "]");
80  						}
81  					}
82  				}
83  			}
84  		}
85  		catch (IOException ioe) {
86  			throw new ItemStreamException("Unable to create file: [" + file.getAbsolutePath() + "]", ioe);
87  		}
88  
89  		if (!file.canWrite()) {
90  			throw new ItemStreamException("File is not writable: [" + file.getAbsolutePath() + "]");
91  		}
92  	}
93  
94  	/**
95  	 * @deprecated use the version with explicit append parameter instead. Here append=false is assumed.
96  	 */
97  	public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) {
98  		setUpOutputFile(file, restarted, false, overwriteOutputFile);
99  	}
100 
101 	/**
102 	 * Create a new file if it doesn't already exist.
103 	 * 
104 	 * @param file the file to create on the filesystem
105 	 */
106 	public static boolean createNewFile(File file) throws IOException {
107 
108 		if (file.exists()) {
109 			return false;
110 		}
111 
112 		try {
113 			return file.createNewFile() && file.exists();
114 		}
115 		catch (IOException e) {
116 			// On some filesystems you can get an exception here even though the
117 			// files was successfully created
118 			if (file.exists()) {
119 				return true;
120 			}
121 			else {
122 				throw e;
123 			}
124 		}
125 
126 	}
127 
128 }