ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to write multiple lines to the text file

Here, we are going to learn how to write multiple lines to the text file in VB.Net?
Submitted by Nidhi, on January 16, 2021

Here, we will use the WriteAllLines() method of the File class to write multiple lines to the specified text file.

Program/Source Code:

The source code to write multiple lines to the text file is given below. The given program is compiled and executed successfully.

'VB.Net program to write multiple lines to the text file.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim line() As String = {
                            "Sample Line 1",
                            "Sample Line 2",
                            "Sample Line 3",
                            "Sample Line 4",
                            "Sample Line 5"
                          }

            File.WriteAllLines("sample.txt", line)
            Console.WriteLine("Data written successfully")

        Catch ex As FileNotFoundException
            Console.WriteLine("File does not exist")
        End Try
    End Sub
End Module

Output:

Data written successfully
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, here we created an array of string, which is initialized with some strings.

File.WriteAllLines("sample.txt", line)
Console.WriteLine("Data written successfully")

In the above code, we write multiple lines to the "sample.txt" file and printed the "Data written successfully" message on the console screen.

VB.Net File Handling Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.