Which way is best to read from a file?

E

ESPN Lover

Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.


using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}



using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
 
K

Ken Dopierala Jr.

Hi,

Both ways are useful depending on what you need to accomplish. The top way
reads the file one line at a time meaning it reads till the line break. If
your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields, then
the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters789

The line above shows a fixed length field file (visualize a space where each
number is). Each first name takes up 7 characters, each last name takes up
10. So if you wanted to get the 3rd first name you would read 7 characters
starting at position 34 (remember streams start at 0). Good luck! Ken.
 
E

ESPN Lover

All I'm trying to do is copy the contents of a file (actually an HTML page
on the file system) into a string. Process the string and output a new
file. So the idea is to read, upfront, the entire file's contents and place
it into a string. This is manipulated and then I'll write out a new file.

Ken Dopierala Jr. said:
Hi,

Both ways are useful depending on what you need to accomplish. The top way
reads the file one line at a time meaning it reads till the line break. If
your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields, then
the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters789

The line above shows a fixed length field file (visualize a space where each
number is). Each first name takes up 7 characters, each last name takes up
10. So if you wanted to get the 3rd first name you would read 7 characters
starting at position 34 (remember streams start at 0). Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

ESPN Lover said:
Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.


using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}



using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
 
K

Ken Dopierala Jr.

Hi,

Then all you have to do is:

sr.BaseStream.Seek(0, SeekOrigin.Begin);
strEntireFile = sr.ReadToEnd();

Put that in the top version of the examples you showed in the place of the
while loop. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

ESPN Lover said:
All I'm trying to do is copy the contents of a file (actually an HTML page
on the file system) into a string. Process the string and output a new
file. So the idea is to read, upfront, the entire file's contents and place
it into a string. This is manipulated and then I'll write out a new file.

Ken Dopierala Jr. said:
Hi,

Both ways are useful depending on what you need to accomplish. The top way
reads the file one line at a time meaning it reads till the line break. If
your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields, then
the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters789

The line above shows a fixed length field file (visualize a space where each
number is). Each first name takes up 7 characters, each last name takes up
10. So if you wanted to get the 3rd first name you would read 7 characters
starting at position 34 (remember streams start at 0). Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

ESPN Lover said:
Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.


using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}



using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top