Different ways to read a text file !!

here are 4 different ways that you can read from a text file

Way 1


using(FileStream fileStream = File.Open("Info.txt", FileMode.OpenOrCreate,
                                                     FileAccess.ReadWrite))

            {

                using (StreamReader reader = new StreamReader(fileStream))

                {

                    while(!reader.EndOfStream)

                    {

                        Console.Out.WriteLine("value = {0}", reader.ReadLine());

                    }

                }

            }



Way 2

            using(StreamReader reader = File.OpenText("Info.txt"))

            {

                while(!reader.EndOfStream)

                {

                    Console.Out.WriteLine("value= {0}", reader.ReadLine());

                }

            }

 


Way 3

            try

            {

                string[] lines = File.ReadAllLines("Info.txt");

                foreach (string line in lines)

                {

                    Console.Out.WriteLine("line = {0}", line);

                }

            }

            catch (IOException exception)

            {

                Console.Write(exception);

            }

 

Way 4

            try

            {

               

               Console.Out.WriteLine(File.ReadAllText("Info.txt"));

            }

            catch (IOException exception )

            {

                Console.Out.WriteLine("exception = {0}", exception);

            }


 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.