Small Basic > Curriculum > Online > Lesson 3.1: File Input and Output
In this lesson, you will learn how to:
Use different properties of the Fileobject.
Use different operations of the Fileobject.
A computer file is a collection of data that your computer stores. In Small Basic, you can work with external files from your program.
By using the File object in Small Basic, you can access information from a file that is stored on your computer. You can also read and write information from and to the file.
The File object includes the following operations and properties:
By using the File object, you can also save and open settings across various sessions of your program.
As you see, you can work with files in many ways by using the File object. Let’s learn about some operations of the File object…
Let’s write a program to gain better understanding of these operations.
FilePath = "C:\temp\TempSubdirectory\my.txt" TextWindow.WriteLine("Write Content = " + File.WriteLine(FilePath, 1, "Shakespeare was a great writer.")) TextWindow.WriteLine("Append Content = " + File.AppendContents(FilePath, "He wrote many plays.")) TextWindow.WriteLine("Read Content = " + File.ReadContents(FilePath))
In this example, you specify the path of a file and write a sentence to it by using the WriteLine operation. Next, you add a sentence to the existing content by using the AppendContents operation. Finally, you read the entire contents of the file by using the ReadContents operation.
If you specify a destination that does not exist, the CopyFile operation will try to create it. If a file of the same name already exists, that operation overwrites the existing file. Before you use this operation, verify that a file of the same name does not already exist in the destination that you specify.
If the CopyFile operation succeeds, “SUCCESS” appears; otherwise, “FAILED” appears.
If the GetFiles operation is successful, it returns the files as an array. Otherwise, “FAILED” appears in the output window.
Let’s write a program to better understand these operations.
sourcefilepath = "C:\temp\TempSubdirectory\my.txt" destinationfilepath ="C:\temp\TempSubdirectory\Move" directorypath = "C:\temp\" TextWindow.WriteLine("Copy file Operation:" + File.CopyFile(sourcefilepath, destinationfilepath)) TextWindow.WriteLine("Files in the directory: " + File.GetFiles(directorypath))
In this example, you copy the specified source file to the specified destination by using the CopyFile operation. You also specify the directory path, and you then display the paths of all files in the output window by using the GetFiles operation.
If the CreateDirectory operation succeeds, “SUCCESS” appears in the output window; otherwise, “FAILED” appears.
If the GetDirectories operation succeeds, a list of directories appears as an array in the output window. Otherwise, “FAILED” appears.
Let’s see how we can apply these operations…
directorypath1 = "C:\temp\Small Basic" TextWindow.WriteLine("Create Directory: " + File.CreateDirectory(directorypath1)) directorypath2 = "C:\temp" TextWindow.WriteLine("Directories: " + File.GetDirectories(directorypath2))
First, you create a directory by using the CreateDirectory operation.
Next, you get the path of all the directories in the location that you specify by using the GetDirectories operation.
By using the LastError property, you can get details about the most recent file-operation error that occurred in your program. This property is quite useful when an error prevents your program from performing a file operation.
FilePath = "C:\temp\TempSubdirect\my.txt" TextWindow.WriteLine("Write Line Operation: " + File.WriteLine(FilePath, 1, "How are you?")) If File.LastError = "" Then TextWindow.WriteLine("Operation Successful") Else TextWindow.WriteLine(File.LastError) EndIf
In this example, you write text to a file at a specific line number that you specify by using the WriteLine operation of the File object.
Next you get the details of the actual error in the program, if any, by using the LastError property of the File object.
Congratulations! Now you know how to:
Write a program that performs the following steps:
This solution requires the file to exist with the specified name in the specified location.
To see the answers to these questions, go to the Answer Key page.
Next Lesson
Ed Price - MSFT edited Revision 2. Comment: PPD section
Fernando Lugão Veltem edited Revision 1. Comment: added toc