• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Update file name  RSS feed

 
Ranch Hand
Posts: 174
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way in which a text can be entered in the middle of current text from a certain character?

i.e. I pick a file "test.docx" and want to rename it "test_updated.docx"

I know I can remove the last 5 characters and then add in "_updated.docx" but if I choose a file with a ".doc" or ".1" extension then I need to search based on the period not the length of the string.

Basically I want to append "_updated" to the file name but then still keep the same extension in place. but I also do not know what the file name will be until it is chosen.

Any advice?
 
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use String#lastIndexOf('.') to find the last period. Then from that position append together substring(0,index) + "_updated" + substring(index).
 
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can insert something into the middle of a text file; do you mean to alter the file's name? Go through this Java™ Tutorials section; look at Copying Paths, etc.
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I don't think you can insert something into the middle of a text file; do you mean to alter the file's name? Go through this Java™ Tutorials section; look at Copying Paths, etc.



The question isn't about inserting into the middle of a file, regardless of the reference to "current text". And it isn't even about changing the name of the file, regardless of the title of the post. It's more basic than that, the OP just needs to generate the String which is going to be the new name of the file.
 
John Morgan
Ranch Hand
Posts: 174
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul you are correct... it was as simple as just converting the string, parsing it, adding the text I needed and then reformatting to complete the name.

Thank you all for your answers here.... now on to my next step.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!