2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Generates a series of filenames for classes that have to save e.g.
26 * snapshots regularly.
28 public class FileNameGenerator {
32 * The number of filenames already returned.
34 private long counter = 0;
36 /** The prefix of the filename */
37 public final String prefix;
39 /** The extension of the filename */
40 public final String ext;
43 // ==================== initialization ==============================
44 // ==================================================================
48 * @param prefix all returned names will be prefixed by this
49 * @param ext will be appended to all returned names
51 public FileNameGenerator(String prefix, String ext) {
58 // ===================== methods ====================================
59 // ==================================================================
63 * Generates a name based on a counter.
64 * The format of the name returned is {@link #prefix} followed by
65 * an 8 digit zero padded number, followed by {@link #ext}.
66 * The first number used is zero.
67 * @return the next filename after increasing the counter
69 public String nextCounterName() {
71 ByteArrayOutputStream baos = new ByteArrayOutputStream();
72 (new PrintStream(baos)).printf("%08d",counter);
74 return prefix+baos.toString()+ext;
77 // ------------------------------------------------------------------
80 public static void main(String args[]) {
82 FileNameGenerator fng = new FileNameGenerator(args[0],args[1]);
83 for(int i=0; i<100; ++i) System.err.println(fng.nextCounterName());