Inserting text at the beginning of an existing text file

G

Guest

Hi,
I'm using Visual Studio 2005. I am developing a web application that uses
the FileUpload control to upload text files to a directory on a webserver.
This works fine.

However, for me to be able to further process the files, I need to insert
some data at the BEGINNING of the text file. All the solutions I've seen
either append the text to the end of the file, or overwrite the data at the
start of the file, or read the entire file into a string
("StreamReader.ReadToEnd"), concatenate, then write the string back to the
file (innefficient).

Please note:
The text files in question could be up to 5000 lines long with each line
being up to 200 characters long (+- 1 million chars), and storing all that in
a string may be a strain on resources, so the solution must be efficient.

Isn't there a way to directly access the file to do the insert?? Something
like:
1. Convert text file to a "direct access file"
2. Insert data at the begining
3. Convert "direct access file" back to a text file

So far my code is as follows for the submit button on the page (see my
commented lines):

Imports System
Imports System.IO

....

Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles butSubmit.Click

.... (working code)

If FileOK Then
Try
Dim strNewFileName As String = path & Now().Ticks.ToString &
"NewFileName.txt"
Dim strRefNo As String = "1234567890"

Me.FileUpload1.PostedFile.SaveAs(strNewFileName)

'The text file now contains:
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.

'this is where I need a code snippet .....
'----
'---- INSERT strRefNo at the BEGINNING of the text file represented by
strNewFileName ----
'----

'After the insert, the text file must be on the webserver and now contain:
'1234567890
'XXXXXXXXX, "qwerty...."
'YYYYYYYYYY, "asdfgh ..."
'etc.

Me.lblResult.ForeColor = Drawing.Color.Black
Me.lblResult.Text = "File successfully uploaded!"
Catch ex As Exception
Me.lblResult.ForeColor = Drawing.Color.Red
Me.lblResult.Text = "File could not be uploaded."
End Try
Else
'display error message
Me.lblResult.ForeColor = Drawing.Color.Red
Me.lblResult.Text = "Cannot accept files of this type."
End If 'FileOK

.... (working code)

End Sub

Any help will be appreciated.
 
G

Guest

Thanks, I know about stringbuilder. I was still hoping for a more efficient
way though.
 
A

Andrew Morton

AHP said:
However, for me to be able to further process the files, I need to
insert some data at the BEGINNING of the text file. All the solutions
I've seen either append the text to the end of the file, or overwrite
the data at the start of the file, or read the entire file into a
string ("StreamReader.ReadToEnd"), concatenate, then write the string
back to the file (innefficient).

The file system doesn't work like that. Compare it with adding a new page of
writing to the start of a notebook. If you'd left space before starting
writing on the first page you could have done it, but then you'd likely have
some empty pages between the new page and the old writing. (If you knew
exactly how much you were going to add, that wouldn't be a problem.)

Write new data to new file
Append old data to new file
Rename old file (just in case something goes wrong)
Rename new file to old filename
Confirm operation worked
If worked, delete old file.

- OR -

Give each file a sequence number and process them in sequence. It will be
faster to rename a few files than rewrite them.

Andrew
 
G

Guest

Thanks Andrew, this can work. I'll change the submission algorithm to cater
for it. Thanks. - AHP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top