1

How can I replace a file name in java I have a file with the following name:

file_1234.dat

I want to change this file to:

file.dat

as well as keep the content of the file as it is.

Thanks

DeveloperRyan
  • 837
  • 2
  • 9
  • 22
Ragnar
  • 645
  • 8
  • 28
  • there are many file in the directory with "_1234" or "_5678" I want to remove the part from _ till . from every file name – Ragnar Apr 14 '16 at 13:33
  • 4
    [duplicate](http://stackoverflow.com/questions/1158777/rename-a-file-using-java) – elreeda Apr 14 '16 at 13:33
  • http://stackoverflow.com/a/20260300/6077352 – PendingValue Apr 14 '16 at 13:33
  • none of them is teh duplicate what i want – Ragnar Apr 14 '16 at 13:37
  • Did you read any of those answers? Although not an exact duplicate, they give you valuable hints on how to achieve what you want. Additionally, your question is not adequate for SO. It is unclear, and it does not show any reasearch nor coding effort. – Seelenvirtuose Apr 14 '16 at 13:38

3 Answers3

3

You could use regular expressions. Look at the example below... this would only work if your your files follow the _1234 pattern:

      String myString = "hello_123.dat";        //you have a string of your choice
    System.out.println(myString);       //prints the string

    String newString = myString.replaceAll("[_\\d]", "");   
    System.out.println(newString);

So you would get this printed out:

hello_123.dat AND hello.dat

If your file names are "file_XMM1234.dat" You can use the following:

yourString= "file_XMM1234.dat"
String newString = yourString.replaceFirst("_[^.]*", "");   

Which will produce: file.dat

deeveeABC
  • 960
  • 3
  • 12
  • 34
  • the file names are file_XMM1234.dat and test_XMM456.dat – Ragnar Apr 14 '16 at 14:07
  • @wearybands, you need to keep in mind that StackOverflow answers are designed to point you (and anyone with a similar problem) in the right direction, but it is up to you to adapt the code to your specific needs. Don't let yourself become a copy/paste programmer - that's the fast track to killing your professional development. – CodeMouse92 Apr 14 '16 at 19:09
1

You can use regex:

String temp = "file_1234.dat";
temp = temp.replaceAll("_\\d+(?=\\.)", "");
System.out.println(temp);
brso05
  • 13,142
  • 2
  • 21
  • 40
  • OP says: "I want to change this file to [...] as well as _keep the content_ of the file as it is." That makes me think, he wants to rename files on his harddisk, not simply do some string operations in memory. The question is therefore unclear, and it does not show an reasearch nor coding effort. Thus, it should not be answered! – Seelenvirtuose Apr 14 '16 at 13:43
  • @Seelenvirtuose it is fairly easy to figure out how to `rename()` a file...The posted links have enough for him to figure it out. This is helping him with the regex part. – brso05 Apr 14 '16 at 13:44
  • @Seelenvirtuose i gave him a nice starting point but if your right he will still have some work/research to do on his own. This site is about helping people... – brso05 Apr 14 '16 at 13:45
  • Who downvoted? At least say why you downvote so the user has a chance to correct their answer(if there even is a valid reason why u downvoted)... – brso05 Apr 14 '16 at 14:30
0

Use the following

Path source = Paths.get("your/path/file_1234.dat");
Files.move(source, source.resolveSibling((source.getFileName().toString().replaceAll("[_\\d]|[A-Z]", ""))));    
M. Suurland
  • 725
  • 12
  • 31