2007-12-23 14:28:04 -05:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
package net.sourceforge.tuned;
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public class FileUtil {
|
2007-12-23 14:28:04 -05:00
|
|
|
|
|
|
|
public final static long KILO = 1024;
|
|
|
|
|
|
|
|
public final static long MEGA = KILO * 1024;
|
|
|
|
|
|
|
|
public final static long GIGA = MEGA * 1024;
|
|
|
|
|
|
|
|
|
|
|
|
public static String formatSize(long size) {
|
|
|
|
if (size >= MEGA)
|
2008-06-21 15:24:18 -04:00
|
|
|
return String.format("%d MB", (double) size / MEGA);
|
2007-12-23 14:28:04 -05:00
|
|
|
else if (size >= KILO)
|
2008-06-21 15:24:18 -04:00
|
|
|
return String.format("%d KB", (double) size / KILO);
|
2007-12-23 14:28:04 -05:00
|
|
|
else
|
2008-06-21 15:24:18 -04:00
|
|
|
return String.format("%d Byte", size);
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-22 17:12:34 -04:00
|
|
|
public static boolean hasExtension(File file, String... extensions) {
|
2008-03-24 11:41:10 -04:00
|
|
|
if (file.isDirectory())
|
2008-03-22 17:12:34 -04:00
|
|
|
return false;
|
|
|
|
|
2008-04-20 12:03:19 -04:00
|
|
|
return hasExtension(file.getName(), extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean hasExtension(String filename, String... extensions) {
|
|
|
|
String extension = getExtension(filename, false);
|
2008-03-22 17:12:34 -04:00
|
|
|
|
|
|
|
for (String ext : extensions) {
|
|
|
|
if (ext.equalsIgnoreCase(extension))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-20 21:45:21 -04:00
|
|
|
public static String getExtension(File file) {
|
|
|
|
return getExtension(file, false);
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-20 21:45:21 -04:00
|
|
|
public static String getExtension(File file, boolean includeDot) {
|
2008-03-24 11:41:10 -04:00
|
|
|
return getExtension(file.getName(), includeDot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getExtension(String name, boolean includeDot) {
|
2007-12-23 14:28:04 -05:00
|
|
|
int dotIndex = name.lastIndexOf(".");
|
|
|
|
|
2008-03-24 11:41:10 -04:00
|
|
|
// .config -> no extension, just hidden
|
2007-12-25 10:30:50 -05:00
|
|
|
if (dotIndex >= 1) {
|
|
|
|
int startIndex = dotIndex;
|
|
|
|
|
|
|
|
if (!includeDot)
|
|
|
|
startIndex += 1;
|
|
|
|
|
|
|
|
if (startIndex <= name.length()) {
|
2007-12-29 08:18:38 -05:00
|
|
|
return name.substring(startIndex, name.length());
|
2007-12-25 10:30:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-19 18:14:38 -04:00
|
|
|
public static String getNameWithoutExtension(String name) {
|
2007-12-23 14:28:04 -05:00
|
|
|
int dotIndex = name.lastIndexOf(".");
|
|
|
|
|
|
|
|
if (dotIndex < 1)
|
|
|
|
return name;
|
|
|
|
|
|
|
|
return name.substring(0, dotIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-24 11:41:10 -04:00
|
|
|
public static String getFileName(File file) {
|
|
|
|
if (file.isDirectory())
|
|
|
|
return getFolderName(file);
|
|
|
|
|
2008-03-19 18:14:38 -04:00
|
|
|
return getNameWithoutExtension(file.getName());
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-24 11:41:10 -04:00
|
|
|
public static String getFolderName(File file) {
|
2008-02-07 18:16:23 -05:00
|
|
|
String name = file.getName();
|
|
|
|
|
|
|
|
if (!name.isEmpty())
|
|
|
|
return name;
|
|
|
|
|
|
|
|
// file might be a drive (only has a path, but no name)
|
|
|
|
return file.toString();
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|
|
|
|
|
2008-03-24 11:41:10 -04:00
|
|
|
|
|
|
|
public static String getFileType(File file) {
|
|
|
|
if (file.isDirectory())
|
|
|
|
return "Folder";
|
|
|
|
|
2008-03-26 20:28:06 -04:00
|
|
|
String extension = getExtension(file.getName(), false);
|
2008-03-24 11:41:10 -04:00
|
|
|
|
|
|
|
if (!extension.isEmpty())
|
|
|
|
return extension;
|
|
|
|
|
|
|
|
// some file with no suffix
|
|
|
|
return "File";
|
|
|
|
}
|
|
|
|
|
2007-12-23 14:28:04 -05:00
|
|
|
}
|