This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inserting new line in the middle of a existing file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inserting new line in the middle of a existing file" Watch "Inserting new line in the middle of a existing file" New topic
Author

Inserting new line in the middle of a existing file

Roy Sin
Greenhorn

Joined: Sep 27, 2001
Posts: 2
Hi,
is there a way that i am able to insert a new line in the middle of an EXISTING text file ???
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well you have several choices.
You can read in the file, one line at a time, and write it to a temp file, and at the correct time shove your own line in there.
When finished reading in the original file, write the temp file back out to it.
You could read in input file into an array, and do an insert at the correct place. This could be the beginning of an Editor of some sort if you stuck on a GUI.
If the text file is small enough, you could read it into one giant string. Then sub-string the part before the addition, add in your stuff, and concatenate in the last part of the string.

"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting new line in the middle of a existing file
 
Similar Threads
File
replacing content in a file
Creating a new file using PrintWriter.
FileReader and FileWriter
mock: java.io.File