Input - Output Exploring Java - Io
Input - Output Exploring Java - Io
FileWriter
PipedOutputStream
BufferedOutputStream
FilterInputStream
PipedReader
BufferedReader
FilterOutputStream
PipedWriter
BufferedWriter
FilterReader
PrintStream
ByteArrayInputStream
FilterWriter
PrintWriter
ByteArrayOutputStrea
m
InputStream
PushbackInputStrea
m
CharArrayReader
InputStreamReader
PushbackReader
CharArrayWriter
LineNumberReader
RandomAccessFile
Console
ObjectInputStream
Reader
DataInputStream
ObjectInputStream.GetFi
eld
SequenceInputStrea
m
DataOutputStream
ObjectOutputStream
SerializablePermissio
n
File
ObjectOutputStream.PutF
ield
StreamTokenizer
FileDescriptor
ObjectStreamClass
StringReader
FileInputStream
ObjectStreamField
StringWriter
FileOutputStream
OutputStream Writer
FilePermission
OutputStreamWriter
2 | Page
FileReader
PipedInputStream
Writer
FileFilter
ObjectInputValidation
DataInput
FilenameFilter
DataOutput
Flushable
Externalizable
ObjectInput
ObjectOutput
ObjectStreamConstants
Serializable
File(String directoryPath)
// directoryPath is the path
name of the file;
File(String directoryPath, String filename) // filename is the name of the
file or subdirectory;
File(File dirObj, String filename)
// dirObj is a File object that
specifies a directory
File(URI uriObj)
3 | Page
Example: The following examples create three files: f1, f2, f3. The first File
object is constructed with a directory path as the only argument. The second
includes two argumentsthe path and the filename. The third includes the file
path assigned to f1 and a filename; f3 refers to the same file as f2
File f1 = new File("/");
File f2 = new File("/","autoexec.bat");
File f3 = new File(f1,"autoexec.bat");
boolean isAbsolute()
long lastModified()
long length()
Description
Returns the name of the file
Returns the name of the Parent
Directory
Returns true if file exists, false
otherwise
Returns the path from the current
directory
Returns the Absolute Path from the root
directory
Returns true if Wrieable, false otherwise
Returns true if Readable, false
otherwise
Returns true if Directory, false
otherwise
Returns true if called on a file, false if
called on a directory. Also isFile()
returns false for some device drivers
and named pipes
Returns true if the file has absolute
path, false if its path is relative
Returns Last Modified time
Returns the length in Bytes
4 | Page
Methods
void deleteOnExit( )
long getFreeSpace( )
long getTotalSpace( )
long getUsableSpace( )
boolean isHidden( )
boolean setLastModified(long millisec)
boolean setReadOnly( )
Description
Removes the file associated with the
invoking object when the Java Virtual
Machine terminates.
Returns the number of free bytes of
storage available on the partition
associated with the invoking object.
Returns the storage capacity of the
partition associated with the invoking
object.
Returns the number of usable free
bytes of storage available on the
partition associated with the invoking
object.
Returns true if the invoking file is
hidden. Returns false otherwise.
Sets the time stamp on the invoking file
to that specified by millisec, which is
the number of milliseconds from
January 1, 1970, Coordinated Universal
Time (UTC).
Sets the invoking file to read-only.
Methods also exist to mark files as readable, writable, and executable. Because
File implements the Comparable interface, the method compareTo( ) is also
supported.
See the Code FileDemo.java
Also read more on java.nio.file class
Directories
A directory is a File that contains a list of other files and directories. When you
create a File object that is a directory, the isDirectory( ) method will return true.
List()
To see other files in a directory, we can use the list() method on that object as:
String[] list() // The list of files is returned in an array of String objects
See the code DirList.java
5 | Page
Using Filename Filter
We often want to limit the number of files returned by the list() method to include
only those files that match a certain filename pattern or filter. To do this we must
use a second form of list() method, shown here:
String[] list(FilenameFilter FFObj)
FFObj is an object of a class that implements the FilenameFilter interface.
FilenameFilter defines only a single method, accept() which is called once for
each file in a list.
Its general form is given here:
boolean accept(File directory, String filename)
The accept( ) method returns true for files in the directory specified by directory
that should
be included in the list and returns false for those files that should be excluded.
Example: See the code OnlyExt.java and DirListOnly.java
listFiles() Alternative
There is a variation to the list( ) method, called listFiles( ), signature for
listfiles() are shown here:
6 | Page
7 | Page
when attempting to open a file. By default, applications run via java do not use a
security manager. Applets could generate a SecurityException.
2. The second approach to closing a stream is to automate the process by using the
try-with-resources statement that was added by JDK 7. The try-withresources statement is an enhanced form of try that has the following form:
try (resource-specification)
{
// use the resource
}
Here, resource-specification is a statement or statements that declares and
initializes a resource, such as a file or other stream-related resource. When the try
blocks
ends,
the
resource
is
automatically
released.
Here are three key points about the try-with-resources statement:
8 | Page
by a