如何将文本附加到现有文本文件 [英] How to append text to an existing text file

查看:60
本文介绍了如何将文本附加到现有文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它按以下格式将数据写入文本文件.

I have a program, which writes data to a text file in the following format.

test1   -    test1     -     test1     -     test1

写完第一行后,文本字段被清除,为另一轮用户输入腾出空间.简单地说,它应该是这样的:

After writing the first line, the text fields are cleared to make space for another round of user input. Simply said, this is how it should look:

test1   -    test1     -     test1     -     test1
test2   -    test2     -     test2     -     test2
test3   -    test3     -     test3     -     test3

这是我的代码

If Not File.Exists(path) Then
    MessageBox.Show("File doesn't exist in the given path", "No File", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
Else

    Dim reader As New StreamReader(path)
    reader = File.OpenText(path)
    Dim content As String = reader.ReadToEnd
    reader.Dispose()
    reader.Close()

    Dim writer As New StreamWriter(path)  'this is where the exception occurs
    writer.Write("Origin : " & Trim(loadOrigin) & vbTab & "-" & vbTab)
    writer.Write("Destination : " & Trim(destination) & vbTab & vbCrLf & vbCrLf)

    writer.Write(Trim(txtCarrier.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtLocation.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtDest.Text) & vbTab & "-" & vbTab)
    writer.Write(Trim(txtNotes.Text) & vbTab & vbCrLf & vbCrLf)

    writer.Close()
    MessageBox.Show("Text written to file", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
    clearFields()

End If

  1. 我使用 StreamReader 读取文本文件中已有的内容并到达其末尾以追加新行.但是,它显示了 IOException 错误消息,消息进程无法访问文件D:\test.txt",因为它正被另一个进程使用.即使在我处置和/或关闭StreamReader之后,它也无济于事.我在这里错过了什么?

  1. I used StreamReader to read the content already inside the text file and reach the end of it to append the new line. However, it shows an IOException error with the message The process cannot access the file 'D:\test.txt' because it is being used by another process. It does not help even after I Dispose and/or Close the StreamReader. What am I missing here?

这段代码能否实现我在上面提到的将多行写入同一个文本文件的最初目的?我需要做任何改变吗?

Would this code fulfill my initial purpose of writing multiple lines to the same text file as I have mentioned above? Do I have to make any changes?

非常感谢大家.

推荐答案

reader.Close() 应该关闭文件并为其释放所有资源.在调用 Dispose 之前尝试关闭文件(根据 MSDN 不需要,因为 Close 方法会为您完成.

reader.Close() should close the file and release all resources for it. Try closing the file prior to calling Dispose (which according to MSDN is not needed because the Close method does that for you.

Dim writer As New StreamWriter(path, True) 将打开文件以添加文本.Dim writer As New StreamWriter(path)Dim writer As New StreamWriter(path, False) 将覆盖文件(如果存在)或创建新文件(如果存在)不存在).

Dim writer As New StreamWriter(path, True) will open the file for appending text. Dim writer As New StreamWriter(path) or Dim writer As New StreamWriter(path, False) will overwrite the file (if it exists) or create a new file (if it does not exist).

如果您仍然遇到异常,请确保您没有在其他地方(例如在记事本中)打开该文件.

If you are still getting the exception, make sure you don't have the file open elsewhere (like in Notepad).

这篇关于如何将文本附加到现有文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆