1 / 16

Using External Files

Using External Files. VB 2010. What is a File?. Variables and Arrays store information in RAM while the program is running. Once the program is closed the information is removed (not really). We need a way to store information even after a program is closed.

cheri
Download Presentation

Using External Files

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. Using External Files VB 2010

  2. What is a File? • Variables and Arrays store information in RAM while the program is running. Once the program is closed the information is removed (not really). We need a way to store information even after a program is closed. • An external file will allow us to store information on some storage device even after a program is closed and then retrieve it when we re-open our program. • We will only use .txt files for our assignments but we are not limited to these.

  3. Three types of Data Files • SequentialFiles - All data is retrieved and stored in a continuous order. They are analogous to cassette tapes. If you want to get to information at the end of file you must go through all the data in front of it. (This is what we will use) • RandomFiles - All data can be retrieved or stored by directly going to where it needs to be. They are analogous to CD. If you want song 3 then you just skip songs 1 and 2 and go straight to 3. • BinaryFiles – We won’t use these, but they are files that look like garbage when you open them. • These three types could also be referred to as: • Output Data Files – These are files where the program is sending information to it. • Input Data Files – These are files where the program is getting information from it. • Typically the same file will be both an output data file and an input data file.

  4. Classes Needed • The following are the tools used by VB.Net to read and write data files. • FileStream – Used when you either need to get information from a file or put information into a file. • StreamReader – Used to get information from a file into the program. • StreamWriter – Used to put information from the program into a file. • FileInfo – We don’t use but it has lots of extra methods to help when dealing with external data files.

  5. Examples • We are going to do 5 examples first before we get some notes. • These examples will count as a practice grade. • Copy the folder named Student Version UsingDataFilesExamples from my VB folder to your folder.

  6. Counts the number of lines in code • counter = System.IO.File.ReadAllLines(strFILENAME).Length

  7. Imports Statement • In order to use certain objects in VB.NET you sometimes need to tell the computer that you need extra “things” to make your project work. The command to provide extra “things” is Imports. • imports System.IO is a command that will provide you with the tools needed to read and write data files. • All imports commands are to be placed above the Public Class somethingstatement. I still make the comments the first line myself. • On a side note, there are tons of commands that are could be imported.

  8. Finding your data files • If external data files are misplaced this can cause your program to crash because it can’t find the data files. • One way to help keep track of your data file location is to make the connection between your program and the data file relative. In other words, if you move the program you also need to move the data file. • The following command returns the file path location of your .exe file. System.AppDomain.CurrentDomain.BaseDirectory() • If this command is used the user will have to always keep the .exe file and the data file together.

  9. Used for creating a stream object for a specific file. A stream is simply a file being stored as a sequence of characters. A graphical look for a text file would be The end of file is represented by a -1 FileStream is needed for both reading and writing data files. Syntax for using a FileStream isDimfsAs New FileStream(File, FileMode, FileAccess) FileStream Class 6 pens binder

  10. FileStream Class continued The following explains the 3 parameters for FileStream • File – The name and path of the file that you need to use. • FileMode – The ones we will use will be • Append – If you want to add more data to a data file • Create – Will create a new data file or erase and create an existing data file. (Typically used for writing to a file) • Open – Will open a file that exists, an error occurs if the file doesn’t exist. (Typically used for reading from a file) • OpenorCreate - Should open a file if it exists; otherwise, a new file should be created • FileAccess – The ones we will use will be • Read – Used to read data into a program • Write – Used to write data from a program into a data file.

  11. FileStream Class continued • Each filestream has a conceptual position or what I call a pointer. The position is simply the spot you are currently working with in the stream itself. • There are several properties and methods for a FileStream but the only two we will use are: • Position – Allows you to move the invisible pointer to where ever you want it to be. • Close() – Closes the file, once a file is closed a new one must be declared. • A good example of filestream is seen in the PartD example, the position is used to move the pointer back to the start of the stream.

  12. Used for creating stream objects by reading either characters or lines of text from a data file. Syntax of declaring a StreamReader objectdimtextFileas new StreamReader(filestream object) Methods and properties we will use include: Readline() – returns the next set of characters up to the line terminator in the stream as a String. Close() – closes the stream reader. EndofStream – a boolean is returned telling you if you have reached the end of a stream. Examples have been given. StreamReader Class

  13. Used for creating stream objects by writing either characters or lines of text to a data file. Syntax of declaring a StreamReader objectdimtextFileas new StreamWriter(filestream object) Methods and properties we will use include: Writeline() – writes a string followed by a line terminator to a stream. Close() – closes the stream writer. Close the streamwriter object before you close the filestream object. Examples have been given. StreamWriter Class

  14. Generic Steps to follow • Import stuff • Create a filepath • Create a FileStream • Usually goes in Form_load • Need to know if you are reading, writing • Create either a StreamReader or StreamWriter • Usually need to count number of lines so an array can be made • Usually read file into array • Close both File Stream and StreamReader or StreamWriter • Run and do stuff in Program • Repeat Steps 3 thru 7 if you need to write info back to file.

  15. Assignments • Modify the High Scores program so scores are kept in a data file. • Sort some text file • Create a login screen and register screen and store information in a data file. • Worksheet????????

  16. Skip for now FileInfo Object

More Related