Você está na página 1de 12

Microsoft Small Basic

File Input and Output


Estimated time to complete this lesson: 1 hour

Click to edit Master subtitle style

7/14/12

File Input and Output


In this lesson, you will learn how to:
Use different properties of the File object. Use different operations of the File object.

7/14/12

The File Object


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: CreateDirecto ry AppendConte nts GetFiles GetDirectorie s ReadContents LastError WriteLine CopyFile DeleteDirecto ry

7/14/12

Operations of the File Object


As you see, you can work with files in many ways by using the File object. Lets learn about some operations of the File object
v

WriteLine You can write a line of text at a line number that you specify in a file by using the WriteLine operation. AppendContents You can add text that you specify at the end of a file by using the AppendContents operation. ReadContents You can read the entire contents of a file by using the ReadContents operation.

7/14/12

Operations of the File Object


Lets write a program to gain better understanding of these operations.

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. 7/14/12

output

Operations of the File Object

CopyFile You can copy the specified file to a destination by using the CopyFile operation. GetFiles You can get a list of all the files in a directory that you specify by using the GetFiles operation.

7/14/12

Operations of the File Object


Lets write a program to better understand these operations.

output

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.

7/14/12

Operations of the File Object


v

CreateDirectory By using this operation, you can create a directory with a name that you specify at a location that you specify. GetDirectories By using this operation, you can get the paths of all the directories in the directory path that you specify.

7/14/12

Operations of the File Object


Lets see how we can apply these operations 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.

output

7/14/12

The LastError Property


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. 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 7/14/12

output

Lets Summarize

Congratulations! Now you know how to:

Use different properties of the File object. Use different operations of the File object.

7/14/12

Show What You Know


Write a program that performs the following steps:
v

Requests a suitable name for a directory from the user, and creates a directory of that name. Downloads a file from the network, and copies it to the new directory. Displays the contents of the downloaded file in the text window. Accepts additional content from the user, and adds it to the file. Displays the final content from the file in the text window.

7/14/12

Você também pode gostar