• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Update file name

 
Ranch Hand
Posts: 193
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: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 65474
249
  • 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: 24635
56
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: 193
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.
 
He's giving us the slip! Quick! Grab this tiny ad!
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!