Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Which way is best to read from a file?

Reply
Thread Tools

Which way is best to read from a file?

 
 
ESPN Lover
Guest
Posts: n/a
 
      10-12-2004
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));
}
}
}
}


 
Reply With Quote
 
 
 
 
Ken Dopierala Jr.
Guest
Posts: n/a
 
      10-12-2004
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.

Bill456Smith56789Mark456Wilson6789Brian56Winters78 9

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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));
> }
> }
> }
> }
>
>



 
Reply With Quote
 
 
 
 
ESPN Lover
Guest
Posts: n/a
 
      10-12-2004
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." <(E-Mail Removed)> wrote in message
news:eV%(E-Mail Removed)...
> 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.
>
> Bill456Smith56789Mark456Wilson6789Brian56Winters78 9
>
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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));
> > }
> > }
> > }
> > }
> >
> >

>
>



 
Reply With Quote
 
Ken Dopierala Jr.
Guest
Posts: n/a
 
      10-13-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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." <(E-Mail Removed)> wrote in message
> news:eV%(E-Mail Removed)...
> > 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.
> >
> > Bill456Smith56789Mark456Wilson6789Brian56Winters78 9
> >
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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));
> > > }
> > > }
> > > }
> > > }
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
ESPN Lover
Guest
Posts: n/a
 
      10-13-2004
Thanks, Ken!! That's what I was after


"Ken Dopierala Jr." <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2011: Which Ruby books have you read? And which would you recommend? Aston J. Ruby 13 03-16-2011 07:36 AM
Which way is the best way to require another file in .rb Eddy Xu Ruby 5 04-11-2008 06:25 PM
Read/write Excel files using Java! Which is the best solution? Ovidiu.Silaghi Java 2 08-25-2006 12:51 AM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
Web Stats? Which to use? Which is best? Familyman HTML 3 02-09-2006 11:05 PM



Advertisments