File Choosers

Overview

Many programs let people open and save files: a text editor opens a story, a drawing app saves a picture, a game loads a saved level. To do this, the program needs to ask the person which file. A file chooser is the window that lets them browse their folders and pick one. In Quorum, the FileChooser class opens the computer's own file window, the same one other programs use, and gives your program back the file that was picked. In this tutorial, you will open a file, save a file, choose a folder, and limit which kinds of files can be picked, all from buttons on a page.

This lesson works only offline, in Quorum Studio on a desktop computer. Unlike the other lessons in this series, its programs can't run in the online code editor, because a program in a web browser can't open your computer's file windows.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Open the computer's open, save and choose folder windows with FileChooser
  • Use the File that comes back, and handle the case where the person cancels
  • Limit the kinds of files a person can pick with a FileFilter
  • Explain why Quorum uses the operating system's own file window
  • Explain what can go wrong with saved files when confidentiality, integrity or availability is ignored

Warm up

Think about the window that opens when you choose "Open" in any app on your computer. Is it the same in every app? Why might it be a good idea for every app on a computer to use the same file window? Think about people who use a keyboard or a screen reader. What would happen if every program made its own?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
FileData saved on a computer under a name, like story.txt.
FolderA place on a computer that holds files and other folders. It is also called a directory.
PathThe list of folders that leads to a file, like /Users/sam/Documents/story.txt.
File extensionThe letters after the last dot in a file's name, like txt in story.txt. They hint at what kind of file it is.
File chooserA window that lets a person browse folders and pick a file or folder.
Operating systemThe main software that runs a computer, like Windows or macOS. It provides the file chooser window.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
FileChooserFileChooser chooserCreates an object that can open the computer's file windows.
OpenFileDialog(FileFilter filter)File file = chooser:OpenFileDialog(filter)Opens a window to pick a file to open. Returns the file or undefined if the person cancels.
SaveFileDialog(FileFilter filter)File file = chooser:SaveFileDialog(filter)Opens a window to pick a name and folder to save to. Returns the file or undefined.
ChooseFolderDialog(File location)File folder = chooser:ChooseFolderDialog(start)Opens a window to pick a folder. It starts in the folder given by location.
FileFilterFileFilter filterCreates a list of file extensions that a file window should allow.
Add(text extension)filter:Add("txt")Adds a file extension to the filter. Leave out the dot.
SetAcceptAll(boolean accept)filter:SetAcceptAll(false)If false, the window allows only the extensions in the filter. A new filter allows every file until you call this.
GetFileName()file:GetFileName()Returns the file's name.
GetAbsolutePath()file:GetAbsolutePath()Returns the file's full path.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-SYS-HW-29

High School · Systems & Security

Differentiate an operating system as a special type of software that manages both the hardware and other software components of a computing system, including handling memory and peripherals.

Only this lesson so far

MS-PRO-TR-19

Middle School · Programming

Use systematic strategies to test, refine, and document changes to a computing technology to meet the intended purpose.

Also in 6 other lessons

S1-SWD-TR-07

Specialty I · Software Development

Design a test plan that exercises program functionality, including edge cases, error conditions, and varied user inputs.

Also in 2 other lessons

MS-SYS-SE-32

Middle School · Systems & Security

Explain the effects of not using the CIA triad when working with data.

Only this lesson so far

HS-SYS-SE-33

High School · Systems & Security

Formulate a solution to a security flaw in a given system.

Only this lesson so far

Every standard, and the lessons that cover it

Explore

Files and file choosers

In Quorum, the File class stands for one file or folder on the computer. A File knows its name and its path, and it can read or write the text inside the file. This tutorial uses only a few of its actions. The File documentation lists the rest.

A FileChooser does not draw its own window. Instead, it asks the operating system to open the same file window that every other program on the computer uses. This has two big benefits:

  • People already know how to use it.
  • It already works with the computer's screen reader and keyboard shortcuts, so you don't have to build that yourself.

Because the window comes from the operating system, it looks different on Windows and on macOS. Your Quorum code is the same on both.

File choosers work in Quorum Studio on a desktop computer. They don't work in the online code editor, which can't reach the files on your computer.

There are three kinds of file window:

  • Open: pick a file that already exists.
  • Save: pick a folder and type a name for a file. The file might not exist yet.
  • Choose folder: pick a folder instead of a file.

Opening a file

In Quorum Studio, make a new project. Under General, choose App. Each window will be opened by a button on the page, with its own behavior. Add a class named OpenFileBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.FileChooser
use Libraries.Interface.Controls.FileFilter
use Libraries.System.File

class OpenFileBehavior is Behavior
    action Run(BehaviorEvent event)
        FileChooser chooser
        FileFilter filter
        filter:Add("txt")
        filter:SetAcceptAll(false)

        File file = chooser:OpenFileDialog(filter)

        if file = undefined
            output "No file was opened."
        else
            output "You opened " + file:GetFileName()
            output "Its full path is " + file:GetAbsolutePath()
            output "It says: " + file:Read()
        end
    end
end

Here is what it does:

  • The FileFilter lists the file extensions the window should allow. Here, it is only txt, so the person can pick text files. Write the extension without the dot.
  • A new filter allows every file. Until you call SetAcceptAll(false), the extensions you added are not used at all. Always call it when you want the filter to limit the choice.
  • OpenFileDialog(filter) opens the window and waits until the person picks a file or cancels.
  • If the person cancels, the result is undefined. Always check for this. If you don't, your program stops with an error the first time someone changes their mind.
  • file:Read() returns all the text in the file.

Saving a file

Add a class named SaveFileBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.FileChooser
use Libraries.Interface.Controls.FileFilter
use Libraries.System.File

class SaveFileBehavior is Behavior
    action Run(BehaviorEvent event)
        FileChooser chooser
        FileFilter filter
        filter:Add("txt")
        filter:SetAcceptAll(false)

        File file = chooser:SaveFileDialog(filter)

        if file = undefined
            output "Nothing was saved."
        else
            if file:Exists()
                output "Replacing " + file:GetFileName()
            end
            file:Write("Hello from my Quorum program!")
            output "Saved to " + file:GetAbsolutePath()
        end
    end
end

In a save window, the person types a new name, or picks a file that is already there. file:Exists() tells you which one happened. file:Write creates the file if needed, and replaces everything in it if it was already there. Most save windows warn the person before they replace a file.

Choosing a folder

Add a class named ChooseFolderBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.FileChooser
use Libraries.System.File
use Libraries.Containers.Array

class ChooseFolderBehavior is Behavior
    action Run(BehaviorEvent event)
        FileChooser chooser
        File start
        File folder = chooser:ChooseFolderDialog(start)

        if folder = undefined
            output "No folder was chosen."
        else
            output "You chose " + folder:GetAbsolutePath()
            Array<File> contents = folder:GetDirectoryListing()
            output "It holds " + contents:GetSize() + " files and folders."
        end
    end
end

ChooseFolderDialog needs a File that says which folder to start in. A new File that has no path refers to your program's working folder, so the window starts there.

The open and save windows can take a starting folder too. Give the folder first and the filter second:

File file = chooser:OpenFileDialog(start, filter)

A filter only makes sense for files, so ChooseFolderDialog doesn't take one.

Adding the buttons to a page

Now replace Main.quorum. It puts one button on the page for each window.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Button

Form form
Page page = form:GetMainPage()

page:AddLabel("Intro", "Each button opens one of your computer's file windows. Results appear in the output window.")

Button open = page:AddButton("Open File")
open:SetDescription("Opens a text file and shows what it says")
OpenFileBehavior openBehavior
open:SetBehavior(openBehavior)

Button save = page:AddButton("Save File")
save:SetDescription("Saves a short message to a text file")
SaveFileBehavior saveBehavior
save:SetBehavior(saveBehavior)

Button folder = page:AddButton("Choose Folder")
folder:SetDescription("Picks a folder and counts what is in it")
ChooseFolderBehavior folderBehavior
folder:SetBehavior(folderBehavior)

form:Display()

The page stacks the buttons in rows and puts them in the Tab order for you, so a keyboard user can reach every one.

Run the program. Use the Save File button first to make a text file, then open it with the Open File button. The output window shows what happened each time.

Keeping saved files safe

Security experts often check how data is protected with three ideas, called the CIA triad. Confidentiality means only the people who should read information can read it. Integrity means information isn't changed or destroyed in ways it shouldn't be. Availability means people can get to their information when they need it (source).

Each one matters when a program saves files. Here is what can happen when a program ignores them:

  • Confidentiality: a person saves a private diary into a folder that everyone on a shared computer can open, and anyone can read it. A program can help by suggesting a private folder, and by never saving somewhere the person didn't choose.
  • Integrity: file:Write replaces everything in a file that is already there. If the program never checks file:Exists() and asks first, one wrong click or key press destroys someone's work. The Option Dialogs tutorial shows how to ask.
  • Availability: if the only copy of a file is on one drive, and that drive breaks or is lost, the file is gone. Keeping a backup copy somewhere else means the work can still be reached.

Engage

In this activity, you write a test plan for the file chooser program, run it, and record what really happens. The goal is to think about everything a person might do with a file chooser, including the unexpected, before you trust your program. A test plan is a list of things to try and what should happen for each. Include at least these cases:

  • picking a file, then pressing Cancel instead;
  • saving with a new name, and saving over a file that's already there;
  • trying to open a file that isn't a .txt file;
  • one test for each part of the CIA triad, such as saving into a folder other people can open, or saving over a file with the only copy of your work;
  • doing everything with only the keyboard (Tab, Space, Enter, the arrow keys and Escape).

Run each test and write down what really happened next to what you expected. If you can, run the same tests on a second operating system, and write down what was different about its file window. Did anything surprise you? For each CIA test, write what the person would lose if the program got it wrong. Then pick one of the made-up flaws from "Keeping saved files safe", such as the diary saved in a shared folder, and write a fix for it: what the program should do differently, and which part of the CIA triad your fix protects. Fix one problem you found, or change the program so it saves text that the person typed into a text field on the page. The Text Fields tutorial shows how to get typed text.

Wrap up

Your program used a window it didn't build. The operating system made it. What are the good and bad sides of handing part of your program to the operating system? What can you no longer control?