View Javadoc

1   package org.lcsim.util.loop;
2   
3   import java.io.BufferedReader;
4   import java.io.BufferedWriter;
5   import java.io.File;
6   import java.io.FileReader;
7   import java.io.FileWriter;
8   import java.io.IOException;
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  /**
13   * Some utitities for dealing with file lists.
14   * @author tonyj
15   */
16  public class FileList
17  {
18     private String title;
19     private List<File> fileList;
20     private final static String TITLE = "Title:";
21     
22     public FileList(List<File> files, String title)
23     {
24        this.fileList = files;
25        this.title = title;
26     }
27     /**
28      * Creates a FileListUtils by reading a file containing a file list.
29      * The file may contain a title (a line starting with "Title:") and any number of files.
30      * Blank lines, or lines beginning with # are ignored.
31      * Files may be absolute, or specified relative to this filelist itself.
32      * @param file The file to read.
33      * @param defaultTitle The title to be used if the file does not contain a title.
34      */
35     public FileList(File file, String defaultTitle) throws IOException
36     {
37        this.fileList = new ArrayList<File>();
38        this.title = defaultTitle;
39        File base = file.getParentFile();
40        BufferedReader fileListReader = new BufferedReader(new FileReader(file));
41        try
42        {
43           for (;;)
44           {
45              String line = fileListReader.readLine();
46              if (line == null) break;
47              line = line.trim();
48              if (line.length() == 0 || line.startsWith("#")) continue;
49              if (line.startsWith(TITLE))
50              {
51                 title = line.substring(TITLE.length()).trim();
52              }
53              else
54              {
55                 File item = new File(base,line);
56                 if (!item.exists() || !item.canRead()) throw new IOException("File "+item+" cannot be read");
57                 fileList.add(item);
58              }
59           }
60        }
61        finally
62        {
63           fileListReader.close();
64        }
65     }
66     /**
67      * Writes this filelist to a file.
68      */
69     public void write(File file) throws IOException
70     {
71        String base = file.getParent();
72        if (!base.endsWith(File.separator)) base += File.separator;
73        
74        BufferedWriter pw = new BufferedWriter(new FileWriter(file));
75        if (title != null)
76        {
77           pw.write(TITLE);
78           pw.write(' ');
79           pw.write(title);
80           pw.newLine();
81        }
82        try
83        {
84           for (File item : fileList)
85           {
86              String filePath = item.getPath();
87              if (filePath.startsWith(base)) filePath = filePath.substring(base.length());
88              pw.write(filePath);
89              pw.newLine();
90           }
91        }
92        finally
93        {
94           pw.close();
95        }
96     }
97     
98     public String getTitle()
99     {
100       return title;
101    }
102    
103    public void setTitle(String title)
104    {
105       this.title = title;
106    }
107    
108    public List<File> getFileList()
109    {
110       return fileList;
111    }
112    
113    public void setFileList(List<File> fileList)
114    {
115       this.fileList = fileList;
116    }
117    
118 }