IOSTREAMSFILES
IOSTREAMSFILES
ORIENTED
PROGRAMMING
BY
S.NAVYA
ASSISTANT PROFESSOR
DEPARTMENT OF CSIT
Stream in java
• In java, the IO operations are performed using the concept of streams. Generally, a
stream means a continuous flow of data. In java, a stream is a logical container of
data that allows us to read from and write to it. A stream can be linked to a data
source, or data destination, like a console, file or network connection by java IO
system. The stream-based IO operations are faster than normal IO operations.
2 int read()It reads the next byte from the input stream.
out.write(data.getBytes());
System.out.println("Writing data into a file is success!");
}
catch(Exception e) {
System.out.println(e);
}
finally {
out.close(); } }}
Character Stream in java
• In java, when the IO stream manages 16-bit Unicode
characters, it is called a character stream. The unicode set is
basically a type of character set where each character
corresponds to a specific numeric value within the given
character set, and every programming language has a character
set.
– In java, the character stream is a 16 bits carrier. The
character stream in java allows us to transmit 16 bits
of data
Contd..
• The java character stream is defined by two abstract
classes, Reader and Writer. The Reader class used for
character stream based input operations, and the Writer
class used for character stream based output operations.
The Reader and Writer classes have
several concreate classes to perform various IO
operations based on the character stream.
•
The following picture shows the classes used for
character stream operations.
Reader class
• The Reader class has defined as an abstract
class, and it has the following methods which
have implemented by its concrete classes.
7 void close()It closes the input stream and also frees any resources
connected with this input stream
Writer class
• The Writer class has defined as an abstract
class, and it has the following methods which
have implemented by its concrete classes.
} }
2. Writing console output using
write() method
• Alternatively, the PrintStream class
provides a method write() to write console
output.
• The write() method take integer as
argument, and writes its ASCII equivalent
character on to the console, it
also accept escape sequences.
public class WritingDemo {
public static void main(String[] args) {
int[] list = new int[26];
for(int i = 0; i < 26; i++) {
list[i] = i + 65; }
for(int i:list) {
System.out.write(i);
System.out.write('\n');
} } }
Files in java
• The File is a built-in class in Java. In java,
the File class has been defined in the
java.io package. The File class represents
a reference to a file or directory. The File
class has various methods to perform
operations like creating a file or
directory, reading from a file,
updating file content, and
deleting a file or directory.
The File class in java has the following
constructors.
S.No. Constructor with Description
1 File(String pathname)It creates a new File instance by converting the given
pathname string into an abstract pathname. If the given string is the empty
string, then the result is the empty abstract pathname.
2 File(String parent, String child)It Creates a new File instance from a parent
abstractpathname and a child pathname string. If parent is null then the new
File instance is created as if by invoking thesingle-argument File constructor on
the given child pathname string.
3 File(File parent, String child)It creates a new File instance from a parent
abstractpathname and a child pathname string. If parent is null then the new
File instance is created as if by invoking thesingle-argument File constructor on
the given child pathname string.
4 File(URI uri)It creates a new File instance by converting the given file: URI
The File class in java has the following methods.
S.No. Methods with Description
1 String getName()It returns the name of the file or
directory that referenced by the current File object.
9 boolean exists()It returns true if the current file or directory exist; otherwise
returns false.
10 boolean canRead()It returns true if and only if the file specified exists and can
be read by the application; false otherwise.
11 boolean canWrite()It returns true if and only if the file specified exists and the
application is allowed to write to the file; false otherwise.
14 boolean createNewFile()It returns true if the named file does not exist and was
successfully created; false if the named file already exists.
15 boolean delete()It deletes the file or directory. And returns true if and only if the
16 void deleteOnExit()It sends a requests that the file or directory needs be deleted
when the virtual machine terminates.
17 boolean mkdir()It returns true if and only if the directory was created; false
otherwise.
18 boolean mkdirs()It returns true if and only if the directory was created, along
with all necessary parent directories; false otherwise.
19 boolean renameTo(File dest)It renames the current file. And returns true if and
only if the renaming succeeded; false otherwise.
20 boolean setLastModified(long time)It sets the last-modified time of the file or
directory. And returns true if and only if the operation succeeded; false otherwise.
21 boolean setReadOnly()It sets the file permission to only read operations; Returns
true if and only if the operation succeeded; false otherwise.
22 String[] list()It returns an array of strings containing names of all the files and
directories in the current directory.
23 String[] list(FilenameFilter filter)It returns an array of strings containing names
of all the files and directories in the current directory that satisfy the specified
filter.
24 File[] listFiles()It returns an array of file references containing names of all the
files and directories in the current directory.
25 File[] listFiles(FileFilter filter)It returns an array of file references containing
names of all the files and directories in the current directory that satisfy the
specified filter.
26 boolean equals(Object obj)It returns true if and only if the argument is not null
and is an abstract pathname that denotes the same file or directory as this abstract
pathname.
27 int compareTo(File pathname)It Compares two abstract pathnames
lexicographically. It returns zero if the argument is equal to this abstract
pathname, a value less than zero if this abstract pathname is lexicographically
less than the argument, or a value greater than zero if this abstract pathname is
lexicographically greater than the argument.
•
File Handling using Byte Stream