Sei sulla pagina 1di 3

COMPUTER PROGRAMMING-2 JAVA

CHAPTER 9 FILES AND STREAMS

A file chooser is a component that provides access to the file system. You can add a file
chooser directly to your application or set it up to open as a dialog. The JFileChooser class
provides three methods that display file chooser dialogs: showOpenDialog with an "Open"
approve button, showSaveDialog with a "Save" approve button, and showDialog with a
user-defined approve button. Keep in mind that a JFileChooser object only allows you to
retrieve the user-selected pathname; any opening, saving, or other file manipulating must
be implemented by you. Another way to present a file chooser is to add an instance of
JFileChooser to a container.

JFileChooser provides a simple mechanism for the user to choose a file.

Constructor and Description

JFileChooser()

Constructs a JFileChooser pointing to the user's default directory.

JFileChooser(File currentDirectory)

Constructs a JFileChooser using the given File as the path.

JFileChooser(File currentDirectory, FileSystemView fsv)

Constructs a JFileChooser using the given current directory and FileSystemView.

JFileChooser(FileSystemView fsv)

Constructs a JFileChooser using the given FileSystemView.

JFileChooser(String currentDirectoryPath)

Constructs a JFileChooser using the given path.

JFileChooser(String currentDirectoryPath, FileSystemView fsv)

Constructs a JFileChooser using the given current directory path and FileSystemView

Low level File i/o

Java uses a hierarchy of classes to deal with different types of data. The InputStream and
OutputStream are abstract classes that use low-level I/O streams to read bytes from or
send them to I/O devices such as files, network sockets, and pipes.

1 Mrs.Anamika Raj,Lecturer,KKU
COMPUTER PROGRAMMING-2 JAVA

LOW-LEVEL AND HIGH-LEVEL STREAMS

Low-level streams. An input or output stream that connects directly to a data source, such
as a file or socket.

High-level streams. An input or output stream that reads or writes to another input or
output stream.

LOW LEVEL STREAMS EXAMPLE

FileInputStream and FileOutputStream. For writing and reading binary data from a file.

ByteArrayInputStream and ByteArrayOutputStream. For writing and reading data from


an array of bytes.

PipedInputStream and PipedOutputStream. For writing and reading data between two
threads.

InputStream and OutputStream. The abstract parent classes

HIGH LEVEL STREAMS EXAMPLE

DataInputStream and DataOutputStream.This is a filter for writing and reading


primitive data types and Strings.

ObjectInputStream and ObjectOutputStream. This is used for serialization and


deserialization of Java objects.

DataInputStream and DataOutputStream.This is a filter for writing and reading


primitive

Object IO

The ObjectOutputStream class

Is part of the java.io package

Is an extension of the OutputStream class, an abstract class that describes the


behavior of an output stream

Object-
ObjectOutputStream

2 Mrs.Anamika Raj,Lecturer,KKU
COMPUTER PROGRAMMING-2 JAVA

Is a high-level class that can be used to send primitive values and "serializable"
objects to a stream. All that is needed for an object to be serializable, is that its class
must implement the Serializable interface. For example, if a Customer class is to be
serializable, its header may be coded

public class Customer implements Serializable

The interface requires no methods.

Many packaged classes are serializable including all wrapper classes, String and
Stringbuffer classes, Vector and Array classes, and the collection classes. In other words, an
entire collection, such as a SortedMap, can be stored as an object on disk!

Has overloaded constructors but the most useful "chains" to an object that descends
from the OutputStream class (such as a FileOutputStream object). For example, if fd
is the reference of a File object

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fd));

will construct a ObjectOutputStream object chained to a FileOutputStream object for


sending primitive values and serializable objects to the file. Because a checked, IOException
may occur, the statement should be enclosed in a try block with an appropriate catch.

Has many useful methods as follows:

Method Usage
writeBoolean() Writes a specified boolean value to the stream
writeByte() Writes a specified byte value to the stream
writeChar() Writes a specified char value to the stream
writeDouble() Writes a specified double value to the stream
writeFloat() Writes a specified float value to the stream
writeInt() Writes a specified int value to the stream
writeLong() Writes a specified long value to the stream
writeObject() Writes a specified serializable object to the stream
writeShort() Writes a specified short value to the stream
Writes a specified String to the stream according to the
writeUTF()
UTF standard

3 Mrs.Anamika Raj,Lecturer,KKU

Potrebbero piacerti anche