Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write text files by VB.NET

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how to read and write VB. NET text files, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Work needs, Xiaobian needs to use VB. NET to read and write text file operations. Programming requires the following:

1. Add program execution errors to the error log

2. Make the editor of Qiaoqiao Reading Network able to read the error log

I remember that when I used vb6.0 before, it was troublesome to operate text files, especially when writing files, I needed to distinguish between sequential files and random files. The textbooks all opened up a chapter specifically for reading and writing text files to explain, which was troublesome enough. Now use. NET to read and write text files, because of the rush of time, did not have time to look at MSDN, and at the same time affected by the 6.0 idea, the problem is complicated, in the additional record to the end of the text file when it can not write down, and then carefully look at the MSDN example, the problem has finally been solved.

Well, the following into the topic, respectively, VB. NET read and write text files common operation to do an example, so as not to take more detours later novice.

We are operating on file streams here, so we need to add:

Imports System.IO

1. Write operation:

Using StreamWriter for System.IO, here is the code:

Dim strFilePath As String = SaveFileDim sw As StreamWriter = New StreamWriter (strFilePath, True)'true' opens the specified file by appending For i = 0 To j temp = i.ToString sw.WriteLine (temp) sw.Flush () Next sw.Close () sw = Nothing

The first thing to note is the constructor new

Public Sub New(path, append, Encoding)

◆path: the complete path to open the file, if the file does not exist, automatically create a new file.

append: The default value is false, indicating whether to open the specified file in append mode. false--If there is a file specified by path, overwrite the original file, otherwise create a new file;true--If there is a file specified by path, open the file and write data at the end of the text by appending data, otherwise create a new file.

Coding: default value is System.Text.Encoding.Default, that is, use the system default encoding, indicating what kind of encoding to write files.

WriterLine (str): Adds a new line to the text and adds a carriage return character to the end of the line

2. Read the operation

Dim line As String

Dim sr As StreamReader = New StreamReader

(strPath, System.Text.Encoding.Default)

Do While sr.Peek() > 0

line = sr.ReadLine()

Loop

sr.Close()

sr = Nothing

'constructor new

Public Sub New(Path, Encoding)

path: The full path to open the file if the file throws an error.

Coding: default value is System.Text.Encoding.Default, that is, use the system default encoding, indicating what kind of encoding to read the file.

The above is "VB. NET how to read and write text files" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report