001 /**
002 * $Id: FileHelper.java,v 1.8 2011/08/01 22:05:43 oboehm Exp $
003 *
004 * Copyright (c) 2008 by Oliver Boehm
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * (c)reated 11.09.2008 by oliver (ob@oasd.de)
019 */
020 package patterntesting.runtime.io;
021
022 import java.io.*;
023
024 import org.apache.commons.io.FilenameUtils;
025
026 /**
027 * The Class FileHelper.
028 *
029 * @author <a href="boehm@javatux.de">oliver</a>
030 * @since 11.09.2008
031 * @version $Revision: 1.8 $
032 */
033 public final class FileHelper {
034
035 /** No need to instantiate it (utility class). */
036 private FileHelper() {}
037
038 /**
039 * Gets the tmpdir.
040 *
041 * @return the tmpdir
042 */
043 public static File getTmpdir() {
044 return new File(System.getProperty("java.io.tmpdir"));
045 }
046
047 /**
048 * Gets the tmpdir.
049 *
050 * @param file the file
051 *
052 * @return the tmpdir
053 */
054 public static File getTmpdir(final File file) {
055 return getTmpdir(file.toString());
056 }
057
058 /**
059 * Gets the tmpdir.
060 *
061 * @param filename the filename
062 *
063 * @return the tmpdir
064 */
065 public static File getTmpdir(final String filename) {
066 return new File(getTmpdir(), filename);
067 }
068
069 /**
070 * Creates a directory in the temp directory.
071 *
072 * @return the directory
073 * @throws IOException Signals that an I/O exception has occurred.
074 */
075 public static File createTmpdir() throws IOException {
076 return createTmpdir("dir", "");
077 }
078
079 /**
080 * Creates a directory in the temp directory.
081 *
082 * @param prefix the prefix
083 * @param suffix the suffix
084 * @return the directory
085 * @throws IOException Signals that an I/O exception has occurred.
086 */
087 public static File createTmpdir(final String prefix, final String suffix) throws IOException {
088 File tmpdir = getTmpdir(prefix + System.currentTimeMillis() + suffix);
089 if (!tmpdir.mkdirs()) {
090 throw new IOException("can't create dir " + tmpdir);
091 }
092 return tmpdir;
093 }
094
095 /**
096 * Gets the home dir.
097 *
098 * @return the home dir
099 */
100 public static File getHomeDir() {
101 return new File(System.getProperty("user.home"));
102 }
103
104 /**
105 * Normalizes a file, analogous to {@link FilenameUtils#normalize(String)}.
106 * But before we normalize it we try to ask to OS for the canonical
107 * path for it.
108 *
109 * @param file the file
110 * @return the file
111 */
112 public static File normalize(final File file) {
113 String filename = null;
114 try {
115 filename = file.getCanonicalPath();
116 } catch (IOException e) {
117 filename = file.getAbsolutePath();
118 }
119 filename = FilenameUtils.normalize(filename);
120 return new File(filename);
121 }
122
123 /**
124 * Comparing two files is not so easy as it seems to be. On unix systems
125 * or Mac OS-X you can see the same file via two different mount points or
126 * as two different hard links. If you are on *nix you can use the inode
127 * to compare but how to get the inode?
128 *
129 * @param f1 file 1
130 * @param f2 file 2
131 * @return true, if successful
132 * @since 1.1
133 */
134 public static boolean equals(final File f1, final File f2) {
135 File one = normalize(f1);
136 File two = normalize(f2);
137 return one.equals(two);
138 }
139
140 }