Reading text files using System.IO and Visual C #. NET

Source: Internet
Author: User
Tags foreach data structures readline static class visual studio
Visual

Reading text files using System.IO and Visual C #. NET

Reading text files in Visual C #. NET to open and read files for read access is a very important part of the input/output (IO) feature, even if you do not need to write to related files. This example opens a file for reading, which is useful for reading a text file, but not for reading a binary file. This example uses one of several methods that can be used to open a file. Although many data structures can be used to store information retrieved from a file, the ArrayList class is the easiest structure to use. To open and read from a file, this example uses objects in the System.IO namespace, especially the System.IO.StreamReader class.

Note: This example requires some form of text (. txt) file to read from.

To load and read text files in Visual C #. NET, follow these steps: 1. Open Visual Studio. NET. Create a new console application in C #. Visual Studio creates a static class and an empty Main () procedure for you.
2. Make sure that the project references at least the System namespace. Use statements are used for the System, System.IO, and System.Collections namespaces, so that the declarations in these namespaces are not required in subsequent code. These statements must precede all other declarations.
Using System;
Using System.IO;
Using System.Collections;
3. To open a file for reading, create a new instance of the StreamReader object and pass the path of the file to the constructor (as shown below):
StreamReader objreader = new StreamReader ("C:\\Test.txt");
4. You need a string variable that stores each row of the file in the variable when it is processed. Because these rows will be added to a ArrayList, you should also declare and create an object of that type.
String Sline= "";
ArrayList arrtext = new ArrayList ();
5. There are many ways to read the file, including the ReadToEnd method of reading the entire file at once. However, in this example, you can use the ReadLine method to read only one row in a file at a time. When the end of the file is reached, this method returns a null value, which can be used to end the loop. When you read each row from a file, you can use the ArrayList Add method to insert the rows into the ArrayList class.
while (sline!= null)
{
sline = objReader.ReadLine ();
if (sline!= null)
Arrtext.add (sline);
}
Objreader.close ();
6. Use the For Each loop to write the newly populated ArrayList content to the console (see below):
foreach (String soutput in Arrtext)
Console.WriteLine (soutput);
Console.ReadLine ();
7. Save and run your code, which will give the console a list of the contents of the file.

Using System;
Using System.IO;
Using System.Collections;

Namespace TextFileReader_csharp
... {
/**////
Summary description for Class1.
///
Class Class1
... {
static void Main (string[] args)
... {
StreamReader objreader = new StreamReader ("C:\test.txt");
String Sline= "";
ArrayList arrtext = new ArrayList ();

while (sline!= null)
... {
sline = objReader.ReadLine ();
if (sline!= null)
Arrtext.add (sline);
}
Objreader.close ();

foreach (String soutput in Arrtext)
Console.WriteLine (soutput);
Console.ReadLine ();
}
}
}

Troubleshooting There are some issues to be aware of when working with file I/O, including the following:? Whenever a file is accessed, the files to be read or written are either not on the system or are in use.
? This example reads the entire file into memory before processing the file. You may experience situations where the file is too large to be stored in memory, or if you do not have permission to access the file.
Any of these situations throws an exception. It is best to always provide a try...catch block to handle these common problems.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.