Difficulty: Medium
Correct Answer: PL/SQL can read and write operating system files by using the UTL_FILE package, which works with database directory objects to open, read, write, and close text files on the server.
Explanation:
Introduction / Context:
Although PL/SQL runs inside the database, many applications need to interact with files on the operating system, for example to import data from text files or to generate flat file reports. Oracle provides controlled ways to do this so that database security is maintained. This question asks which mechanism PL/SQL uses to read and write operating system files.
Given Data / Assumptions:
Concept / Approach:
Oracle provides the UTL_FILE package for file input and output. Before using UTL_FILE, a database directory object must be created and granted to the schema so that the database knows which physical folder is allowed. PL/SQL code then calls UTL_FILE.FOPEN to open a file in a given directory and mode, UTL_FILE.PUT_LINE or UTL_FILE.GET_LINE to write or read lines, and UTL_FILE.FCLOSE to close the file. This controlled mechanism allows administrators to manage which directories the database can access, maintaining a secure boundary between the database engine and the file system.
Step-by-Step Solution:
Step 1: Create a directory object such as CREATE DIRECTORY reports_dir AS '/u01/app/reports'; and grant read or write privileges on that directory to the application schema.Step 2: In PL/SQL, call UTL_FILE.FOPEN with the directory object name, file name, and mode such as 'w' for writing or 'r' for reading.Step 3: For writing, use UTL_FILE.PUT_LINE or related routines to send text lines to the file. For reading, use UTL_FILE.GET_LINE in a loop to fetch data from the file.Step 4: When finished, call UTL_FILE.FCLOSE or FCLOSE_ALL to ensure that the file handles are released.Step 5: Handle exceptions such as NO_DATA_FOUND or UTL_FILE errors to manage end of file and file permission problems gracefully.
Verification / Alternative check:
To verify, write a small PL/SQL procedure that uses UTL_FILE to create a test file and then read it back. After running the procedure, confirm that the file exists in the configured directory and that its contents match what the code wrote. Any errors will help check whether directory privileges and file paths are configured correctly.
Why Other Options Are Wrong:
Option B claims that PL/SQL cannot access files, which is false because UTL_FILE is designed specifically for this purpose. Option C suggests automatic file operations on table creation, which is not how Oracle behaves. Option D confuses file generation with database export tools and is not a realistic approach for simple file operations.
Common Pitfalls:
A common issue is misconfiguring directory objects or forgetting to grant appropriate privileges, which leads to runtime errors. Another pitfall is not closing files properly, potentially exhausting file handles. Developers should also be careful with file paths and character encodings, especially when moving code between environments.
Final Answer:
PL/SQL can read and write operating system files by using the UTL_FILE package, which works with database directory objects to open, read, write, and close text files on the server.
Discussion & Comments