130 likes | 142 Views
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
E N D
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 • We will only concentrate on simple text files
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!!
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!!!
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”)
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(“”)
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
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
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()
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
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”)
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()