How to append "test" to a filename (after path and before extension)?

Discussion in 'Java' started by Jochen Brenzlinger, Aug 25, 2011.

  1. Let's start with a filename which is stored in a String variable e.g.

    String fn = "D:\project\java\testproj\log2011.log"

    I want to append "test" to the file basename but keep path and extension.
    The resulting filename for the example above should be:

    string fn2 = "D:\project\java\testproj\log2011test.log"

    How can I do this programmatically from Java?

    Jochen
     
    Jochen Brenzlinger, Aug 25, 2011
    #1
    1. Advertising

  2. On 25.08.2011 11:50, Jochen Brenzlinger wrote:
    > Let's start with a filename which is stored in a String variable e.g.
    >
    > String fn = "D:\project\java\testproj\log2011.log"
    >
    > I want to append "test" to the file basename but keep path and extension.
    > The resulting filename for the example above should be:
    >
    > string fn2 = "D:\project\java\testproj\log2011test.log"
    >
    > How can I do this programmatically from Java?


    Typically I use regular expressions for this, here's just one way:

    import java.io.File;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public final class NameMungle {

    public static void main(String[] args) {
    final String[] testData = { "a/b", "a/b/c.d", "a/b/c.d.e" };
    final String append = "test";
    final Pattern pat = Pattern.compile("([^.]*)(\\..*)");

    for (final String s : testData) {
    final File f = new File(s);
    final String basename = f.getName();
    final Matcher m = pat.matcher(basename);

    final File o = new File(f.getParentFile(),
    m.matches()
    ? m.group(1) + append + m.group(2)
    : basename + append);

    System.out.println(f + " -> " + o);
    }
    }
    }

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Aug 25, 2011
    #2
    1. Advertising

  3. Jochen Brenzlinger

    Eric Sosman Guest

    On 8/25/2011 5:50 AM, Jochen Brenzlinger wrote:
    > Let's start with a filename which is stored in a String variable e.g.
    >
    > String fn = "D:\project\java\testproj\log2011.log"


    Aside: Those backslash characters need some attention ...

    > I want to append "test" to the file basename but keep path and extension.
    > The resulting filename for the example above should be:
    >
    > string fn2 = "D:\project\java\testproj\log2011test.log"
    >
    > How can I do this programmatically from Java?


    A bare-bones approach:

    int dot = fn.lastIndexOf('.');
    String fn2 = fn.substring(0, dot) + "test" + fn.substring(dot);

    In actual use you'd want some sanity checking to be sure the '.'
    is in fact present and is after the last '\', so you wouldn't
    get fooled by

    D:\project\java\testproj\config
    or
    D:\project\java\testproj.old\data

    You could guard against such things by using fn.lastIndexOf('\\')
    to locate the rightmost back-slash and checking that the '.' does
    in fact appear even further right. However, I think you're better
    off using the File class to chop up and reassemble file names in a
    (mostly) platform-independent way, e.g.

    File base = new File(fn);
    String name = base.getName(); // handles / or \ or whatever
    int dot = name.lastIndexOf('.');
    String newname = (dot < 0) ? name + "test"
    : name.substring(0, dot) + "test" + name.substring(dot);
    File test = new File(base.getParentFile(), newname);
    String fn2 = test.getPath(); // if needed

    This may seem like a lot of running around for little effect, but
    it protects you from some unpleasant surprises in the long run.

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 25, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Wagner

    sys.path.append('/my/new/path')

    Jeff Wagner, Nov 24, 2003, in forum: Python
    Replies:
    3
    Views:
    530
    Patrick Useldinger
    Nov 24, 2003
  2. Lad
    Replies:
    8
    Views:
    440
    Tom Anderson
    Dec 9, 2005
  3. Ed
    Replies:
    10
    Views:
    45,949
    alok000707
    Jul 13, 2010
  4. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  5. endy_tj
    Replies:
    1
    Views:
    147
    Bret Pettichord
    May 7, 2006
Loading...

Share This Page