Debian Codegraph @

src/dr/util/FileHelpers.java

raw symbols SCIP
1 /*
2 * FileHelpers.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.io.*;
29
30 /**
31 * @author Joseph Heled
32 * Date: 15/04/2008
33 */
34 public class FileHelpers {
35
36 public static final String FILE_NAME = "fileName";
37
38 /**
39 * @param file the file to read the line numbers from
40 * @return Number of lines in file
41 * @throws IOException low level file error
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 * Resolve file from name.
58 * <p/>
59 * Keep A fully qualified (i.e. absolute path) as is. A name starting with a "./" is
60 * relative to the master directory (set by FileHelpers.setMasterDir).
61 * Any other name is stripped of any directory
62 * component and placed in the "user.dir" directory.
63 *
64 * @param fileName an absolute or relative file name
65 * @return a File object resolved from provided file name
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 // directory where beast xml file resides
103 private static File masterDirectory = null;
104
105 public static void setMasterDir(File fileName) {
106 masterDirectory = fileName;
107 }
108 }