1 / 13

CS 102

CS 102. File IO. Overview. File Input/Output StreamWriter StreamReader Text Files Binary Files. File IO. Significant programs will require you to read/write data from data files Configuration files Data Files For complex applications you will use Databases Microsoft SQL Server/Access

kellyreyes
Download Presentation

CS 102

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 102 File IO

  2. Overview • File Input/Output • StreamWriter • StreamReader • Text Files • Binary Files

  3. File IO • Significant programs will require you to read/write data from data files • Configuration files • Data Files • For complex applications you will use Databases • Microsoft SQL Server/Access • We will only concentrate on simple text files

  4. File Structure • Simple text files are nothing more than a series of characters Hello there. This is text in a text file This is a second line in the same text file. • At the end of the file there is a character called EOF (End Of File). You can’t see it • But you can test for it!!

  5. How to Use A File • To use a file, you must: • Include a special Imports statement at the top of your program: Imports System.IO • Open the file • If the file doesn’t exist, you can’t open it • Then you must create it • Data can be written to the file (always at the end of the file!!) • Data can be read from the end of the file • Files can have Random Access to write/read from any point of the file – beyond the scope of this class • These are called Binary files. • Can’t view them in an editor!! • When finished with the file, close it!!!

  6. StreamWriter • A Visual Basic Object that you declare (Dim) so that you can write to a file Dim filMyFile As StreamWriter • To create a new text file, use File.CreateText filMyFile = File.CreateText(“myfile.txt”) • To add to an existing file, use AppendText filMyFile = File.AppendText(“myfile.txt”)

  7. Writing To A Text File • Once the file is open, you write to it with the Write/WriteLine statements filMyFile.WriteLine(“This is a line of text!!”) • The Write statement does NOT add a newline after the text (does not press the return key) • The WriteLine statement adds a newline after the text • To put a blank line in the file, just add a WRiteLine without text filMyFile.WriteLine(“”)

  8. Closing A File • You must always close files when finished with it • Use the Close statement filMyFile.Close() • Only close files if they’re open • Once you close it, you can’t close it again until it’s open

  9. Appending Files • You can add text to an existing file • Open it with the AppendText command • Then just write to it as you would any other text file • When finished, just close it as you would any other text file

  10. Sample File Creation Dim filTestFile As StreamWriter filTestFile = File.CreateText(“mytest.txt”) filTestFile.WriteLine(“This is the first line of text!”) filTestFile.WriteLine(“Here is the second!”) filTestFile.WriteLine(“”) filTestFile.WriteLine(“The previous line was blank!!”) filTestFile.Close() filTestFile = File.AppendText(“mytest.txt”) filTestFile.WriteLine(“”) filTestFile.WriteLine(“Oops. We forgot to add this line the first time!”) filTestFile.Close()

  11. StreamReader • Object in Visual Basic used to read text files • Declare the variable with a Dim statement Dim filTestFile As StreamReader • To open the file, use the OpenText method: filTestFile = File.OpenText(filTestFile) • To read data from the file, use the ReadLine method Dim txtALine As String txtALine = filTestFile.ReadLine() • Data can ONLYbe read in a forward direction

  12. Other Useful File Methods • Check to see if a file exists or not (FileExists) If File.Exists(strFileName) Then ‘ Do something here End If • You can line things up nicely with vbTab • It is a string, which adds one tab to the text lstMyListBox.items.Add(“Name: “ & vbTab & vbTab & “Bill Clinton”) lstMyListBox.items.Add(“Name: “ & vbTab & vbTab & “George Bush”)

  13. Other Useful File Methods • You can find if you’re at the end of the file with Peek Dim myFile As StreamReader Dim strMyStr As String myFile = File.OpenText(“grades.txt”) Do Until myFile.Peek = -1 strMyStr = myFile.ReadLine() lstMyListBox.Items.Add(strMyStr) Loop myFile.Close()

More Related