Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

Reading and Writing Text to Files - Page 4
       by kirupa  |  29 March 2007

In the previous page you learned how to write files, and I began talking about how to check if a file exists. In this page, I'll continue from where we left off and talk more about checking whether files exist.

Something to remember is that a file is created or overwritten when your StreamWriter is initialized with the path to your file. Therefore, something like the following will not work like you would want to:

string filePath = "C:\\Users\\Kirupa\\Desktop\\foo.txt";
 
StreamWriter foo = new StreamWriter(filePath);
 
if (File.Exists(filePath))
{
// File exists
}
else
{
// File does not exist
}

The reason is that the moment your StreamWriter object foo is initialized, the file pointing to by filePath will be created or overwritten. Our File.Exists boolean will always return true because no matter what you reasonably do, a file at filePath will exist.

If you want to avoid overwriting an existing file, the following approach works better:

string filePath = "C:\\Users\\Kirupa\\Desktop\\foo.txt";
 
StreamWriter foo;
 
if (File.Exists(filePath))
{
// File exists
// Don't do anything.
}
else
{
// File does not exist
foo = new StreamWriter(filePath);
foo.Write("Blarg. Zorb. Zeeb. Foo.");
foo.Close();
}

In the above code, a file is created only if File.Exists returns false. Notice that our StreamWriter object foo isn't being initialized until our code reaches the File.Exists == false condition.


Asynchronous File Handling
Both the StreamReader and StreamWriter classes work asynchronously. That's a cool sounding word that basically means that you can do other things when using StreamReader or StreamWriter. To take a real world example, if you were opening a huge text file, you can continue to use your application and everything will seem to work fine without suffering delays or lags associated with reading your file.

If both of those classes were not asynchronous, then you would have to use Threads to ensure any time consuming reading or writing task operates semi-independently from other tasks. Threads is an interesting topic that I will save for a later article.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 
Cheers!
Kirupa Chinnathambi
kirupaBlog

 

 

1 | 2 | 3 | 4


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com