View Javadoc

1   package baseCode.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   /***
7    * Copyright (c) 2004 Columbia University
8    * 
9    * @author Pavlidis
10   * @author Will Braynen
11   * @version $Id: FileTools.java,v 1.7 2005/03/21 18:01:04 pavlidis Exp $
12   */
13  public class FileTools {
14  
15     protected final static String PNG_EXTENSION = "png";
16     protected final static String GIF_EXTENSION = "gif";
17     protected final static String TXT_EXTENSION = "txt";
18     protected final static String[] XML_EXTENSIONS = {
19           "XML", "xml"
20     };
21  
22     protected final static String[] IMAGE_EXTENSIONS = {
23           PNG_EXTENSION, GIF_EXTENSION, "PNG", "GIF"
24     };
25     protected final static String[] DATA_EXTENSIONS = {
26           TXT_EXTENSION, "TXT"
27     };
28     // default values
29     public final static String DEFAULT_DATA_EXTENSION = TXT_EXTENSION;
30     public final static String DEFAULT_IMAGE_EXTENSION = PNG_EXTENSION;
31     public final static String DEFAULT_XML_EXTENSION = "xml";
32  
33     /***
34      * @param file
35      * @throws IOException
36      */
37     public static void checkPathIsReadableFile( String file ) throws IOException {
38        File infile = new File( file );
39        if ( !infile.exists() || !infile.canRead() ) {
40           throw new IOException( "Could not find file: " + file );
41        }
42     }
43  
44     /***
45      * Returns the extension of a file.
46      * 
47      * @param filename
48      * @return @return
49      */
50     public static String getExtension( String filename ) {
51  
52        String extension = null;
53        int i = filename.lastIndexOf( '.' );
54  
55        if ( i > 0 && i < filename.length() - 1 ) {
56           extension = filename.substring( i + 1 ).toLowerCase();
57        }
58        return extension;
59     } // end getExtension
60  
61     /***
62      * @param filename
63      * @return
64      */
65     public static String getWithoutExtension( String filename ) {
66  
67        String[] s = filename.split( "." );
68        String extension = s[s.length - 1];
69        String filenameWithoutExtension = filename.substring( filename.length()
70              - extension.length() - 1, filename.length() - 1 );
71  
72        return filenameWithoutExtension;
73     } // end getFilenameWithoutExtension
74  
75     /***
76      * @param filename
77      * @param newExtension
78      * @return the new filename with the changed extension, but does not modify the <code>filename</code> parameter.
79      */
80     public static String changeExtension( String filename, String newExtension ) {
81  
82        String filenameWithoutExtension = getWithoutExtension( filename );
83        return ( filenameWithoutExtension + newExtension );
84     } // end getWithChangedExtension
85  
86     /***
87      * @param filename
88      * @return
89      */
90     public static boolean hasImageExtension( String filename ) {
91  
92        String extension = getExtension( filename );
93        if ( extension != null ) {
94           for ( int i = 0; i < FileTools.IMAGE_EXTENSIONS.length; i++ ) {
95              if ( FileTools.IMAGE_EXTENSIONS[i].equals( extension ) ) {
96                 return true;
97              }
98           }
99        }
100       return false;
101    } // end hasImageExtension
102 
103    /***
104     * @param filename
105     * @return
106     */
107    public static boolean hasXMLExtension( String filename ) {
108       String extension = getExtension( filename );
109       if ( extension != null ) {
110          for ( int i = 0; i < FileTools.XML_EXTENSIONS.length; i++ ) {
111             if ( FileTools.XML_EXTENSIONS[i].equals( extension ) ) {
112                return true;
113             }
114          }
115       }
116       return false;
117    }
118 
119    /***
120     * @param filename
121     * @return
122     */
123    public static boolean hasDataExtension( String filename ) {
124 
125       String extension = getExtension( filename );
126       if ( extension != null ) {
127          for ( int i = 0; i < FileTools.DATA_EXTENSIONS.length; i++ ) {
128             if ( FileTools.DATA_EXTENSIONS[i].equals( extension ) ) {
129                return true;
130             }
131          }
132       }
133       return false;
134    } // end hasImageExtension
135 
136    /***
137     * @param filename
138     * @return the new filename with the added extension, but does not modify the <code>filename</code> parameter.
139     */
140    public static String addImageExtension( String filename ) {
141       return ( filename + "." + FileTools.DEFAULT_IMAGE_EXTENSION );
142    }
143 
144    /***
145     * @param filename
146     * @return the new filename with the added extension, but does not modify the <code>filename</code> parameter.
147     */
148    public static String addDataExtension( String filename ) {
149       return ( filename + "." + FileTools.DEFAULT_DATA_EXTENSION );
150    }
151 
152    /***
153     * @param dirname directory name
154     * @return
155     */
156    public static boolean testDir( String dirname ) {
157       if ( dirname != null && dirname.length() > 0 ) {
158          File f = new File( dirname );
159          if ( f.isDirectory() && f.canRead() ) {
160             return true;
161          }
162       }
163       return false;
164    }
165 
166    /***
167     * @param filename
168     * @return
169     */
170    public static boolean testFile( String filename ) {
171       if ( filename != null && filename.length() > 0 ) {
172          File f = new File( filename );
173          if ( f.isFile() && f.canRead() ) {
174             return true;
175          }
176       }
177       return false;
178    }
179    
180    /***
181     * 
182     * @param file
183     * @return
184     */
185    public static boolean testFile( File file ) {
186       if ( file != null   ) {
187          if ( file.isFile() && file.canRead() ) {
188             return true;
189          }
190       }
191       return false;
192    }
193 
194 }