Download presentation
Presentation is loading. Please wait.
1
Sequential files creation & writing
Sangeetha Parthasarathy 07/16/2001
2
Files Java files are sequential stream of bytes ending with a file-
marker. When a file is opened an object is created and a stream is associated with the object. Streams provide communication channels between program and a file or device. For example: System.out enables the program to write output data to screen.
3
To perform file processing in java, we must import java.io.
This package includes definitions for the stream classes such as FileInputStream(input from file) and FileOutputStream (output to file). Files are opened by creating an object by creating objects of these stream class that inherit from InputStream and OutputStream. To perform input for different data types, the following stream class DataInputStream, DataOutputStream, ObjectInputStream and ObjectOutputStream. Java provides With File class to provide information about the files. Java provides abstract class InputStream and OutputStream define methods for performing input and output respectively with their derived classes override their methods.
4
File File(String pathname)- Creates a new File instance by converting the given pathname string into an pathname. public boolean canRead(): Tests whether the application can read the file denoted by this abstract pathname. Returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise. public boolean canWrite(): Tests whether the application can modify to the file denoted by this abstract pathname. Returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
5
public boolean exists(): Tests whether the file denoted by this abstract pathname exists. Returns true if and only if the file denoted by this abstract pathname exists; false otherwise. public boolean isDirectory(): Tests whether the file denoted by this abstract pathname is a directory. Returns true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise. public boolean isFile(): Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory. Any non-directory file created by a Java application is guaranteed to be a normal file. Returns true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise.
6
public boolean isHidden(): Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. Returns true if and only if the file denoted by this abstract pathname is hidden according to the conventions of the underlying platform. public long lastModified(): Returns the time that the file denoted by this abstract pathname was last modified. Returns A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970). public long length(): Returns the length of the file denoted by this abstract pathname. Returns The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.
7
public boolean delete(): Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Returns true if and only if the file or directory is successfully deleted; false otherwise. Public String[] list(): Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Returns An array of strings naming the files and directories in the directory denoted by this abstract pathname.
8
public File[] listFiles(): Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, Returns An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. public boolean mkdir(): Creates the directory named by this abstract pathname. Returns true if and only if the directory was created; false otherwise. public boolean renameTo(File dest): Renames the file denoted by this abstract pathname. Returns true if and only if the renaming succeeded; false otherwise.
9
public boolean setLastModified(long time): Sets the last-modified time of the file or directory named by this abstract pathname. public boolean setReadOnly(): Marks the file or directory named by this abstract pathname so that only read operations are allowed. public String getPath(): Converts this abstract pathname into a pathname string. The resulting string uses the default name-separator character to separate the names in the name sequence. public String getAbsolutePath(): Returns the absolute pathname string of this abstract pathname.
10
FileInputStream A file output stream is an output stream for writing data to a File.What files are available or may be created depends on the host environment. Class FileOutputStream java.lang.Object +-java.io.OutputStream +-java.io.FileOutputStream
11
FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(String name) Creates an output file stream to write to the file with the specified name. FileOutputStream(String name, boolean append) public void close() throws IOException: Closes this file output stream and releases any system resources associated with this stream. This file output stream may no longer be used for writing bytes.
12
public void write(int b) throws IOException: Writes the specified byte to this file output stream. Implements the write method of OutputStream. public void write(byte[] b) throws IOException: Writes b.length bytes from the specified byte array to this file output stream. public void write(byte[] b, int off, int len) throws IOException: Writes len bytes from the specified byte array starting at offset off to this file output stream.
13
FileInputStream A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. Class FileInputStream java.lang.Object +java.io.InputStream +-java.io.FileInputStream
14
FileInputStream(File file)
Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system. FileInputStream(String name) Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system. public int available() throws IOException: Returns the number of bytes that can be read from this file input stream without blocking.
15
public void close() throws IOException: Closes this file input stream and releases any system resources associated with the stream. public long skip(long n) throws IOException: Skips over and discards n bytes of data from the input stream. public int read() throws IOException: Reads a byte of data from this input stream. This method blocks if no input is yet available. public int read(byte[] b) throws IOException: Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available. public int read(byte[] b, int off, int len) throws IOException: Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.