csse221 section 2. using jfilechooser to ease use of file i/o in gui programs review of text-based...
TRANSCRIPT
![Page 1: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/1.jpg)
JAVA FILE I/OCSSE221 Section 2
![Page 2: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/2.jpg)
Overview
Using JFileChooser to ease use of file I/O in GUI programs
Review of text-based file I/O Streams/Readers/Writers Using the File class to manage file
systems Object-based, compressed, and byte-
based file I/O
![Page 3: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/3.jpg)
JfileChooser
When coding a GUI, JAVA provides a JFileChooser in swing. JFileChooser is very customizable, with features such as filters for file extensions when opening and saving.
Another advantage to JFileChooser is the ability to have it filter the list of files to only a few. If you wanted to pass only .txt files into a program, you would use a FileFilter or FilenameFilter with the JFileChooser.
![Page 4: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/4.jpg)
JFileChooser – Markov Example
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser();
int returnVal = jfc.showOpenDialog(jfc); //Here to make sure a valid file is selected
if (returnVal == JFileChooser.APPROVE_OPTION) {
String inputFile = jfc.getSelectedFile().toString();
int n = 1; // prefix-length
int maxWordsGen = 100;
int maxCharsPerLine = 20;
Markov m = new Markov(inputFile, n, maxWordsGen, maxCharsPerLine);
}
else{
System.out.println("Failed because no file was selected");
}
}
Markov will not work outright with this, as a few changes will need to be made to how Markov interprets file location, but the correct file will be selected.
![Page 5: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/5.jpg)
JFileChooser – FileFilter
The FileFilter Interface requires the method: public boolean accept(File f)
To use a FileFilter with the JFileChooser from the last example, the following code would be placed after JFileChooser jfc was initialized:
jfc.addChoosableFileFilter(new FileFilter(){…});
jfc.setAcceptAllFileFilterUsed(false);
![Page 6: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/6.jpg)
Text-Based File I/O Review
Basic text-based file I/O review:
File name=new File(“file.txt”);
BufferedReader fileIn=null;
try{
fileIn=new BufferedReader(new FileReader(name));
//read your data in
}catch(IOException e){
//error handling
}finally{
fileIn.close();
}
![Page 7: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/7.jpg)
I/O Streams
Streams are the basis of all I/O in Java (terminal, web sockets, files, etc…).
A Stream is created for EITHER input or output, NOT both
The Reader and Writer classes we commonly use are extension of the Stream classes
The Scanner used with Markov is an example of a “simplified” input class, though it does not directly extend a Stream
The Stream concept is important, and we will revisit it later when discussing other types of I/O
![Page 8: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/8.jpg)
Buffered Classes
Buffered Classes: a Buffered class extends its lower counterpart (i.e. BufferedReader extends FileReader).
Buffering allows reading of more than a single “piece” of data at a time.
Whether or not you want to use Buffered classes is largely a design/functionality decision
![Page 9: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/9.jpg)
Implementation Rules
Correct order of instantiation must be followed A Stream/Reader/Writer must be initialized inside a
try/catch/finally block that catches an IOException. The “outermost” Stream/Reader/Writer needs to
be declared and set to null BEFORE the try/catch block
A Stream/Reader/Writer needs to be closed (preferably in the finally block), or else:Files written will not “finalize” and will not appear
correctly. Files read may be “locked” into the program, and not be available elsewhere.
Only a limited number of Streams can be open at once.
![Page 10: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/10.jpg)
File
The File class creates an object representing a file on the user’s system
Constructors:new File(String name);new File(File parent, String suffix);new File(File parent, File suffix);
Useful methods:exists()createNewFile() isDirectory() length()* lastModified()* list() listFiles()
![Page 11: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/11.jpg)
Demo
Review of text-based file I/O Using the File class to define and
organize files Using the File class to gather data about
and modify files
![Page 12: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/12.jpg)
Activity
Take the text from textFile.txt, and append it to the end of data/existingData.txt
Next, move the textFile.txt file to the data/newData directory as newFile.dat (HINT: the older textFile.txt may be removed in the process)
BONUS: within data/newData, create a directory named for each line of data/existingData.txt
This exercise builds off the file structure already created in the fileSystemExample() and textIOExample() so keep in mind the location and state of the files after that code is run! !
![Page 13: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/13.jpg)
Object-Based I/O
Saving/retrieving data for use in a program can become complicated, using Object I/O can save you from having to convert/organize all your data for text-based I/O
Uses ObjectOuputStream and ObjectInputStream classes
Objects may be written to a file as long as they implement the Serializable interface.
See example code in full version of Demo
![Page 14: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/14.jpg)
Compressed File I/O
Compressed I/O allows you to write data to.zip files which can save space and make archiving easier.
Uses GZIPOutputStream and GZIPInputStream, and is implemented after the basic/buffered level, but before the specific level.
See example code in full version of Demo
![Page 15: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/15.jpg)
Byte-Based I/O
Byte-based I/O allows Java to handle reading/writing of files that are neither text-based nor object based.
Uses DataInputStream and DataOutputStream
NOTE: unlike text-based and object-based, information read into the program can only be worked with if the decoding is known.
Again, see example code in full version of Demo
![Page 16: CSSE221 Section 2. Using JFileChooser to ease use of file I/O in GUI programs Review of text-based file I/O Streams/Readers/Writers Using the](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649d1f5503460f949f2bd3/html5/thumbnails/16.jpg)
Code Quick Reference
BufferedInputStream read(); close();
BufferedReader read(); readLine(); close();
ObjectInputStream readObject(); close();
DataInputStream readFully(byte[] b);
BufferedOutputStream write();
BufferedWriter write(); newLine(); close();
ObjectOutputStream writeObject(); close();
DataOutputStream write();