Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7370
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: How to append "test" to a filename (after path and before extension)? |
| Date | Thu, 25 Aug 2011 13:02:07 +0200 |
| Lines | 47 |
| Message-ID | <9bmodmF30aU1@mid.individual.net> (permalink) |
| References | <4e561ae9$0$7610$9b4e6d93@newsspool1.arcor-online.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net IteYQQRQ8UE3FHSugkNHqQUSa1BdQnmoj6vHg3bXEAJ2cs2oY= |
| Cancel-Lock | sha1:DF0a+MyGbTZYbgCkAxXuk3OV2Wg= |
| User-Agent | Mozilla/5.0 (Windows NT 6.0; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 |
| In-Reply-To | <4e561ae9$0$7610$9b4e6d93@newsspool1.arcor-online.net> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7370 |
Show key headers only | View raw
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/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
How to append "test" to a filename (after path and before extension)? jochen2@brenz.com (Jochen Brenzlinger) - 2011-08-25 09:50 +0000 Re: How to append "test" to a filename (after path and before extension)? Robert Klemme <shortcutter@googlemail.com> - 2011-08-25 13:02 +0200 Re: How to append "test" to a filename (after path and before extension)? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-25 07:30 -0400
csiph-web