001 /*
002 Galois, a framework to exploit amorphous data-parallelism in irregular
003 programs.
004
005 Copyright (C) 2010, The University of Texas at Austin. All rights reserved.
006 UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS SOFTWARE
007 AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR ANY
008 PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF PERFORMANCE, AND ANY
009 WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE.
010 NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF THE
011 SOFTWARE OR DOCUMENTATION. Under no circumstances shall University be liable
012 for incidental, special, indirect, direct or consequential damages or loss of
013 profits, interruption of business, or related expenses which may arise from use
014 of Software or Documentation, including but not limited to those resulting from
015 defects in Software and/or Documentation, or loss or inaccuracy of data of any
016 kind.
017
018 File: InputOutput.java
019
020 */
021
022
023
024 package util;
025
026 import java.io.BufferedWriter;
027 import java.io.File;
028 import java.io.FileWriter;
029 import java.io.FilenameFilter;
030 import java.io.IOException;
031 import java.util.Arrays;
032 import java.util.Collection;
033 import java.util.Collections;
034 import java.util.List;
035
036 /**
037 * Helper methods related to the filesystem, but not included in
038 * {@link java.nio} or {@link java.io}
039 */
040
041 public final class InputOutput {
042
043 /**
044 * File separator, OS-dependent
045 */
046 public static final String FILE_SEPARATOR = System.getProperty("file.separator");
047 /**
048 * Line separator, OS-dependent
049 */
050 public static final String LINE_SEPARATOR = System.getProperty("line.separator");
051
052 /**
053 * Returns a list containing the absolute path of every file that is contained
054 * in the given directory and matches the given regular expression.
055 *
056 * @param directory absolute path representing a directory
057 * @param suffix regular expression
058 * @return full paths of all files matching the given criteria
059 */
060 public static Collection<String> getFilePathsMatching(String directory, final String suffix) {
061 FilenameFilter filter = new FilenameFilter() {
062 public boolean accept(File dir, String name) {
063 return name.matches(suffix);
064 }
065 };
066 return getFilePaths(directory, filter);
067 }
068
069 /**
070 * Returns a list containing the absolute path of every file that is contained
071 * in the given directory and has the given suffix
072 *
073 * @param directory absolute path representing a directory.
074 * @param suffix suffix we are looking for.
075 * @return full paths of all files matching the given criteria.
076 */
077 public static Collection<String> getFilePathsEndingWith(String directory, final String suffix) {
078 FilenameFilter filter = new FilenameFilter() {
079 public boolean accept(File dir, String name) {
080 return name.endsWith(suffix);
081 }
082 };
083 return getFilePaths(directory, filter);
084 }
085
086 /**
087 * Returns a list containing the absolute path of every file that is contained
088 * in the given directory and has the given prefix
089 *
090 * @param directory absolute path representing a directory
091 * @param prefix prefix we are looking for
092 * @return full paths of all files matching the given criteria
093 */
094 public static Collection<String> getFilePathsStartingWith(String directory, final String prefix) {
095 FilenameFilter filter = new FilenameFilter() {
096 public boolean accept(File dir, String name) {
097 return name.startsWith(prefix);
098 }
099 };
100 return getFilePaths(directory, filter);
101 }
102
103 private static Collection<String> getFilePaths(String directory, FilenameFilter filter) {
104 File inputDirectory = new File(directory);
105 String[] children = inputDirectory.list(filter);
106 if (children == null) {
107 throw new RuntimeException("Input directory: " + inputDirectory + " does not exist or is not a directory.");
108 }
109 List<String> result = Arrays.asList(children);
110 Collections.sort(result);
111 for (int i = 0; i < result.size(); i++) {
112 String fullPath = directory + "/" + result.get(i);
113 result.set(i, fullPath);
114 }
115 return result;
116 }
117
118 /**
119 * Appends the given text into the designated file
120 *
121 * @param filePath full path to a file; if the file does not exist, it is created.
122 * @param text text to write in the file
123 * @throws IOException if there is an error while writing or closing the file
124 */
125 public static void write(final String filePath, final String text) throws IOException {
126 write(filePath, text, true);
127 }
128
129 /**
130 * Write the given text into the designated file.
131 *
132 * @param filePath full path to a file; if the file does not exist, it is created.
133 * @param text text to write in the file
134 * @param append whether to append (true) to or overwrite (false) the contents of
135 * the file
136 * @throws IOException if there is an error while writing or closing the file
137 */
138 public static void write(final String filePath, final String text, boolean append) throws IOException {
139 BufferedWriter out = new BufferedWriter(new FileWriter(filePath, append));
140 out.write(text);
141 out.close();
142 }
143 }