|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| FileTools.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 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 | 0 |
public static void checkPathIsReadableFile( String file ) throws IOException { |
| 38 | 0 |
File infile = new File( file );
|
| 39 | 0 |
if ( !infile.exists() || !infile.canRead() ) {
|
| 40 | 0 |
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 | 0 |
public static String getExtension( String filename ) { |
| 51 |
|
|
| 52 | 0 |
String extension = null;
|
| 53 | 0 |
int i = filename.lastIndexOf( '.' );
|
| 54 |
|
|
| 55 | 0 |
if ( i > 0 && i < filename.length() - 1 ) {
|
| 56 | 0 |
extension = filename.substring( i + 1 ).toLowerCase(); |
| 57 |
} |
|
| 58 | 0 |
return extension;
|
| 59 |
} // end getExtension
|
|
| 60 |
|
|
| 61 |
/**
|
|
| 62 |
* @param filename
|
|
| 63 |
* @return
|
|
| 64 |
*/
|
|
| 65 | 0 |
public static String getWithoutExtension( String filename ) { |
| 66 |
|
|
| 67 | 0 |
String[] s = filename.split( "." );
|
| 68 | 0 |
String extension = s[s.length - 1]; |
| 69 | 0 |
String filenameWithoutExtension = filename.substring( filename.length() |
| 70 |
- extension.length() - 1, filename.length() - 1 ); |
|
| 71 |
|
|
| 72 | 0 |
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 | 0 |
public static String changeExtension( String filename, String newExtension ) { |
| 81 |
|
|
| 82 | 0 |
String filenameWithoutExtension = getWithoutExtension( filename ); |
| 83 | 0 |
return ( filenameWithoutExtension + newExtension );
|
| 84 |
} // end getWithChangedExtension
|
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* @param filename
|
|
| 88 |
* @return
|
|
| 89 |
*/
|
|
| 90 | 0 |
public static boolean hasImageExtension( String filename ) { |
| 91 |
|
|
| 92 | 0 |
String extension = getExtension( filename ); |
| 93 | 0 |
if ( extension != null ) { |
| 94 | 0 |
for ( int i = 0; i < FileTools.IMAGE_EXTENSIONS.length; i++ ) { |
| 95 | 0 |
if ( FileTools.IMAGE_EXTENSIONS[i].equals( extension ) ) {
|
| 96 | 0 |
return true; |
| 97 |
} |
|
| 98 |
} |
|
| 99 |
} |
|
| 100 | 0 |
return false; |
| 101 |
} // end hasImageExtension
|
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* @param filename
|
|
| 105 |
* @return
|
|
| 106 |
*/
|
|
| 107 | 0 |
public static boolean hasXMLExtension( String filename ) { |
| 108 | 0 |
String extension = getExtension( filename ); |
| 109 | 0 |
if ( extension != null ) { |
| 110 | 0 |
for ( int i = 0; i < FileTools.XML_EXTENSIONS.length; i++ ) { |
| 111 | 0 |
if ( FileTools.XML_EXTENSIONS[i].equals( extension ) ) {
|
| 112 | 0 |
return true; |
| 113 |
} |
|
| 114 |
} |
|
| 115 |
} |
|
| 116 | 0 |
return false; |
| 117 |
} |
|
| 118 |
|
|
| 119 |
/**
|
|
| 120 |
* @param filename
|
|
| 121 |
* @return
|
|
| 122 |
*/
|
|
| 123 | 0 |
public static boolean hasDataExtension( String filename ) { |
| 124 |
|
|
| 125 | 0 |
String extension = getExtension( filename ); |
| 126 | 0 |
if ( extension != null ) { |
| 127 | 0 |
for ( int i = 0; i < FileTools.DATA_EXTENSIONS.length; i++ ) { |
| 128 | 0 |
if ( FileTools.DATA_EXTENSIONS[i].equals( extension ) ) {
|
| 129 | 0 |
return true; |
| 130 |
} |
|
| 131 |
} |
|
| 132 |
} |
|
| 133 | 0 |
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 | 0 |
public static String addImageExtension( String filename ) { |
| 141 | 0 |
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 | 0 |
public static String addDataExtension( String filename ) { |
| 149 | 0 |
return ( filename + "." + FileTools.DEFAULT_DATA_EXTENSION ); |
| 150 |
} |
|
| 151 |
|
|
| 152 |
/**
|
|
| 153 |
* @param dirname directory name
|
|
| 154 |
* @return
|
|
| 155 |
*/
|
|
| 156 | 0 |
public static boolean testDir( String dirname ) { |
| 157 | 0 |
if ( dirname != null && dirname.length() > 0 ) { |
| 158 | 0 |
File f = new File( dirname );
|
| 159 | 0 |
if ( f.isDirectory() && f.canRead() ) {
|
| 160 | 0 |
return true; |
| 161 |
} |
|
| 162 |
} |
|
| 163 | 0 |
return false; |
| 164 |
} |
|
| 165 |
|
|
| 166 |
/**
|
|
| 167 |
* @param filename
|
|
| 168 |
* @return
|
|
| 169 |
*/
|
|
| 170 | 0 |
public static boolean testFile( String filename ) { |
| 171 | 0 |
if ( filename != null && filename.length() > 0 ) { |
| 172 | 0 |
File f = new File( filename );
|
| 173 | 0 |
if ( f.isFile() && f.canRead() ) {
|
| 174 | 0 |
return true; |
| 175 |
} |
|
| 176 |
} |
|
| 177 | 0 |
return false; |
| 178 |
} |
|
| 179 |
|
|
| 180 |
/**
|
|
| 181 |
*
|
|
| 182 |
* @param file
|
|
| 183 |
* @return
|
|
| 184 |
*/
|
|
| 185 | 0 |
public static boolean testFile( File file ) { |
| 186 | 0 |
if ( file != null ) { |
| 187 | 0 |
if ( file.isFile() && file.canRead() ) {
|
| 188 | 0 |
return true; |
| 189 |
} |
|
| 190 |
} |
|
| 191 | 0 |
return false; |
| 192 |
} |
|
| 193 |
|
|
| 194 |
} |
|
||||||||||