This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Is there a way to append text to a File on a specific line or location.  RSS feed

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I know how to append text to a File using the true argument but I would like to be able to append to a certain line In the file.
Basically I've made a simple html page with Image links to different sites. I'm trying to make an app that will take a new site as Input and append It to the html file before the </body> tags.
 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know exactly where then maybe RandomAccessFile might work.
I however think that you are better off using APIs that understand the structure of the file you are trying to edit e.g DOM libraries like jdom.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have little to no knowledge of what a DOM library Is. I'll check It out. Thanks!
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What If I create a new file and use BufferedReader to copy the contents up to the </body> tag , replace It with the original and then append the data with a new </body> tag at the end.Something along those lines.
 
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean append or insert text? The two are different. I know you can append text with a File Writer (look for its constructors), but look in the Java® Tutorials because there is probably a more modern way to do it; FileWriter is now considered legacy code.

I do not know any way to insert or prepend code, however, in a text file. There might be something in the Tutorials “trail” which I gave you the link to.
You can copy the entire file into a List<String> and explore the Strings and add entries to the middle of the List.
You can also create a Map<String, Integer> where the Integer represents line numbers, which makes finding particular text much faster. There are also some readers or writers which record line numbers.
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tutorials about random access files.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Thanks for the Info. I've come up with the above code so far.At least It works, though be It messy!
 
Bartender
Posts: 20775
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Append" means to add at (after) the end of the file. Many systems support that, often with an "append" mode for opening the file for writing.

"Insertion" - in the middle or at any place before the end of the file is more complicated, as is deletion.

For a random-access file, inserts, deletes, and updates are fairly simple, but text files are not normally organized that way. And since anything other than a linear series of (probably) variable-length text lines is not going to be supported by the java text io streams and writers, that typically means that you're going to have to create a temporary copy of the file, writing the original contents and inserting (and/or deleting/updating) lines as you copy. You then close the original and temp files and either copy the temp file over the original input file or delete the original file and rename the temp file to have the original file's name (this is popular since it's less work, but means that the temp file must be in the same directory as the original file).
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't create a new Writer for every line. That will give dreadfully slow performance.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.I'll clear that up
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also use try‑with‑resources rather than explicit close() calls. That ensures the readers and writers are closed even if an Exception occurs. It is particularly important to be sure that writers are closed, otherwise they may not be flushed and some text may vanish.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!