| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.util; |
| 27 |
|
| 28 |
import java.io.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class FileHelpers { |
| 35 |
|
| 36 |
public static final String FILE_NAME = "fileName"; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static int numberOfLines(File file) throws IOException { |
| 44 |
RandomAccessFile randFile = new RandomAccessFile(file, "r"); |
| 45 |
long lastRec = randFile.length(); |
| 46 |
randFile.close(); |
| 47 |
FileReader fileRead = new FileReader(file); |
| 48 |
LineNumberReader lineRead = new LineNumberReader(fileRead); |
| 49 |
lineRead.skip(lastRec); |
| 50 |
int count = lineRead.getLineNumber() - 1; |
| 51 |
fileRead.close(); |
| 52 |
lineRead.close(); |
| 53 |
return count; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static File getFile(String fileName, String prefix) { |
| 68 |
final boolean localFile = fileName.startsWith("./"); |
| 69 |
final boolean relative = masterDirectory != null && localFile; |
| 70 |
if (localFile) { |
| 71 |
fileName = fileName.substring(2); |
| 72 |
} |
| 73 |
|
| 74 |
if (prefix != null) { |
| 75 |
fileName = prefix + fileName; |
| 76 |
} |
| 77 |
|
| 78 |
final File file = new File(fileName); |
| 79 |
final String name = file.getName(); |
| 80 |
String parent = file.getParent(); |
| 81 |
|
| 82 |
if (!file.isAbsolute()) { |
| 83 |
String p; |
| 84 |
if (relative) { |
| 85 |
p = masterDirectory.getAbsolutePath(); |
| 86 |
} else { |
| 87 |
p = System.getProperty("user.dir"); |
| 88 |
} |
| 89 |
if (parent != null && parent.length() > 0) { |
| 90 |
parent = p + '/' + parent; |
| 91 |
} else { |
| 92 |
parent = p; |
| 93 |
} |
| 94 |
} |
| 95 |
return new File(parent, name); |
| 96 |
} |
| 97 |
|
| 98 |
public static File getFile(String fileName) { |
| 99 |
return getFile(fileName, null); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
private static File masterDirectory = null; |
| 104 |
|
| 105 |
public static void setMasterDir(File fileName) { |
| 106 |
masterDirectory = fileName; |
| 107 |
} |
| 108 |
} |