View Javadoc
1   //******************************************************************************
2   //
3   // File:    Files.java
4   // Package: edu.rit.io
5   // Unit:    Class edu.rit.io.Files
6   //
7   // This Java source file is copyright (C) 2007 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.io;
41  
42  import java.io.File;
43  
44  /**
45   * Class Files provides static methods for various file related operations.
46   *
47   * @author Alan Kaminsky
48   * @version 26-Nov-2007
49   */
50  public class Files {
51  
52  // Prevent construction.
53      private Files() {
54      }
55  
56  // Exported operations.
57      /**
58       * Append the given rank to the given file. The rank goes before the file
59       * extension if any. For example,
60       * <code>Files.fileForRank(new&nbsp;File("image.pjg"),2)</code> returns a File
61       * whose name is <code>"image_2.pjg"</code>;
62       * <code>Files.fileForRank(new&nbsp;File("image"),2)</code> returns a File whose
63       * name is <code>"image_2"</code>.
64       *
65       * @param file File.
66       * @param rank Rank.
67       * @return File with rank appended.
68       */
69      public static File fileForRank(File file,
70              int rank) {
71          return fileAppend(file, "_" + rank);
72      }
73  
74      /**
75       * Append the given rank to the given file name. The rank goes before the
76       * file extension if any. For example,
77       * <code>Files.fileNameForRank("image.pjg",2)</code> returns
78       * <code>"image_2.pjg"</code>; <code>Files.fileNameForRank("image",2)</code> returns
79       * <code>"image_2"</code>.
80       *
81       * @param filename File name.
82       * @param rank Rank.
83       * @return File name with rank appended.
84       */
85      public static String fileNameForRank(String filename,
86              int rank) {
87          return fileNameAppend(filename, "_" + rank);
88      }
89  
90      /**
91       * Append the given suffix to the given file. The suffix goes before the
92       * file extension if any. For example,
93       * <code>Files.fileAppend(new&nbsp;File("image.pjg"),"_new")</code> returns a
94       * File whose name is <code>"image_new.pjg"</code>;
95       * <code>Files.fileAppend(new&nbsp;File("image"),"_new")</code> returns a File
96       * whose name is <code>"image_new"</code>.
97       *
98       * @param file File.
99       * @param suffix Suffix.
100      * @return File with suffix appended.
101      */
102     public static File fileAppend(File file,
103             String suffix) {
104         return new File(fileNameAppend(file.getPath(), suffix));
105     }
106 
107     /**
108      * Append the given suffix to the given file name. The suffix goes before
109      * the file extension if any. For example,
110      * <code>Files.fileNameAppend("image.pjg","_new")</code> returns
111      * <code>"image_new.pjg"</code>; <code>Files.fileNameAppend("image","_new")</code>
112      * returns <code>"image_new"</code>.
113      *
114      * @param filename File name.
115      * @param suffix Suffix.
116      * @return File name with suffix appended.
117      */
118     public static String fileNameAppend(String filename,
119             String suffix) {
120         int i = filename.lastIndexOf('.');
121         return i == -1
122                 ? filename + suffix
123                 : filename.substring(0, i) + suffix + filename.substring(i);
124     }
125 
126     /**
127      * Prepend the given prefix to the given file. The prefix goes after the
128      * directory if any. For example,
129      * <code>Files.filePrepend(new&nbsp;File("/home/ark/image.pjg"),"new_")</code>
130      * returns a File whose name is <code>"/home/ark/new_image.pjg"</code>;
131      * <code>Files.filePrepend(new&nbsp;File("image.pjg"),"new_")</code> returns a
132      * File whose name is <code>"new_image.pjg"</code>. The system-dependent file
133      * name separator character is used to detect the end of the directory if
134      * any.
135      *
136      * @param file File.
137      * @param prefix Prefix.
138      * @return File with prefix prepended.
139      */
140     public static File filePrepend(File file,
141             String prefix) {
142         return new File(fileNamePrepend(file.getPath(), prefix));
143     }
144 
145     /**
146      * Prepend the given prefix to the given file name. The prefix goes after
147      * the directory if any. For example,
148      * <code>Files.fileNamePrepend("/home/ark/image.pjg","new_")</code> returns
149      * <code>"/home/ark/new_image.pjg"</code>;
150      * <code>Files.fileNamePrepend("image.pjg","new_")</code> returns
151      * <code>"new_image.pjg"</code>. The system-dependent file name separator
152      * character is used to detect the end of the directory if any.
153      *
154      * @param filename File name.
155      * @param prefix Prefix.
156      * @return File name with prefix prepended.
157      */
158     public static String fileNamePrepend(String filename,
159             String prefix) {
160         int i = filename.lastIndexOf(File.separatorChar);
161         return i == -1
162                 ? prefix + filename
163                 : filename.substring(0, i + 1) + prefix + filename.substring(i + 1);
164     }
165 
166 // Unit test main program.
167 //	/**
168 //	 * Unit test main program.
169 //	 * <P>
170 //	 * Usage: java edu.rit.io.Files <I>filename</I> <I>rank</I>
171 //	 */
172 //	public static void main
173 //		(String[] args)
174 //		throws Exception
175 //		{
176 //		String filename = args[0];
177 //		int rank = Integer.parseInt (args[1]);
178 //		File file = new File (filename);
179 //		System.out.println
180 //			("Files.fileForRank (file, rank) = " +
181 //			  Files.fileForRank (file, rank));
182 //		System.out.println
183 //			("Files.fileNameForRank (filename, rank) = " +
184 //			  Files.fileNameForRank (filename, rank));
185 //		}
186 //	/**
187 //	 * Unit test main program.
188 //	 * <P>
189 //	 * Usage: java edu.rit.io.Files <I>filename</I> <I>str</I>
190 //	 */
191 //	public static void main
192 //		(String[] args)
193 //		throws Exception
194 //		{
195 //		String filename = args[0];
196 //		String str = args[1];
197 //		File file = new File (filename);
198 //		System.out.println
199 //			("Files.fileAppend (file, str) = " +
200 //			  Files.fileAppend (file, str));
201 //		System.out.println
202 //			("Files.fileNameAppend (filename, str) = " +
203 //			  Files.fileNameAppend (filename, str));
204 //		System.out.println
205 //			("Files.filePrepend (file, str) = " +
206 //			  Files.filePrepend (file, str));
207 //		System.out.println
208 //			("Files.fileNamePrepend (filename, str) = " +
209 //			  Files.fileNamePrepend (filename, str));
210 //		}
211 }