Essentials, Part 1, Lesson 6: File Access and Permissions
So far, you have learned how to retrieve and handle a short text string entered from the keyboard into a simple graphical user interface (GUI). But programs also retrieve, handle, and store data in files and databases.
This lesson expands the examples from previous lessons to perform basic file access using the application programming interfaces (APIs) in the java.io package. It also shows you how to grant applets permission to access specific files, and how to restrict an application so it has access to specific files only.
File Access by Applications
The Java 2 Platform software provides a rich range of classes for reading character or byte data into a program, and writing character or byte data out to an external file, storage device, or program. The source or destination might be on the local computer system where the program is running or anywhere on the network.
This section shows you how to read data from and write data to a file on the local computer system. See The Java Tutorial trail on Reading and Writing for information on transferring data between programs, between a program and memory, and performing operations such as buffering or character encoding on data as it is read or written.
- Reading: A program opens an input stream on the file and reads the data in serially (in the order it was written to the file).
When Application Starts
When Button Clicked
The conversion from the SwingUI.java program for Lesson 4 to the FileIO.java program for this lesson primarily involves the constructor and the actionPerformed method as described here.
Constructor and Instance Variable Changes
A JTextfield instance variable is added to the class so the constructor can instantiate the object and the actionPerformed method can access the text the end user types into it.
The constructor instantiates the JTextField with a value of 20. This value tells the Java platform the number of columns to use to calculate the preferred width of the field. Lower values result in a narrower display, and likewise, higher values result in a wider display.
The text label is added to the North section of the BorderLayout so the JTextField can be added to the Center section.
The actionPerformed method uses the FileInputStream and FileOutputStream classes to read data from and write data to a file. These classes handle data in byte streams, as opposed to character streams, which are shown in the applet example. A more detailed explanation of the changes to the method implementation follows the code.
To write the end user text to a file, the text is retrieved from the textField and converted to a byte array.
Next, a File object is created for the file to be written to and used to create a FileOutputStream object.
The code to open a file for reading is similar. To read text from a file, a File object is created and used to create a FileInputStream object.
Next, a byte array is created the same size as the file into which the file contents are read.
Finally, the byte array is used to construct a String object, which is used to create the text for the label component. The FileInputStream is closed when the operation completes.