0% found this document useful (0 votes)
2 views

java 5

The document outlines a course on Java Programming, focusing on Stream-based I/O and the Collections Framework, which are essential for handling input/output operations and data management in Java. It details various classes and methods for reading and writing data, as well as file handling techniques. Additionally, it provides references for textbooks and online resources for further learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java 5

The document outlines a course on Java Programming, focusing on Stream-based I/O and the Collections Framework, which are essential for handling input/output operations and data management in Java. It details various classes and methods for reading and writing data, as well as file handling techniques. Additionally, it provides references for textbooks and online resources for further learning.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

JAVA PROGRAMMING

(Professional Elective-1)
Course Code:125BK

Prerequisites: Programming for Problem


Solving
Academic Year : 2024-25 Lectures by: Dr. E. Gouthami, Asst. Prof.
III Year B.Tech. EEE EEE
I-Semester Email: gouthami.erp@gnits.ac.in
UNIT 5: (~ 9 Lecture Hours)
Stream based I/O (java.io) The Collections Framework
• The Stream Classes –
• Byte Streams and Character Streams,
(java.util)
• Reading Console Input and Writing • Collections Overview,
Console Output, • Collection Interfaces,
• File class, • The Collection Classes –
• Reading and Writing files, • ArrayList,
• Random Access File operations, Generics, • LinkedList,
• Enumerations.
• Iterator,
• The For-Each alternative,
• HashTable,
• Stack,
• StringTokenizer,
• Random Scanner.
Stream based I/O (java.io)
The Collections Framework (java.util)
• Stream-based I/O in Java, part of the java.io package, is essential for efficiently
handling input and output operations, especially with large amounts of data.
• It enables programs to process data in a continuous flow or "stream" instead of loading
everything into memory at once, which is particularly useful when reading/writing
files, network communication, or working with data streams.

• The Java Collections Framework (java.util package) is a core library that provides
efficient and flexible data structures for handling and manipulating collections of
objects.
• This framework includes several classes and interfaces for data storage and retrieval,
making it a powerful tool for organizing and managing data in Java applications.
Stream based I/O (java.io)
The Collections Framework (java.util)
➢ 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.
➢ The Stream is defined in the java.io package
➢ In java, the stream-based IO operations are performed using two separate
streams - input stream and output stream.
➢ The java stream is composed of bytes.
Stream based I/O (java.io)
The Collections Framework (java.util)

Source Destination

File
File

Console 00011000 Program 00011000 Console

Socket Socket
I/O Stream Hierarchy

IOStreams

ByteStream CharacterStream

InputStream OutputStream Reader Writer


ByteStream Classes

Socket
CharacterStream Classes

Socket
Java Predefined streams
• System contains predefined Stream variables in, out and err.

Socket
• These fields are declared as public and static

• System.out refers to standard output stream (default is console)

• System.in refers to standard input stream (default is keyboard)

• System.err refers to standard error stream (default is console)

• System.in is the object of type InputStream

• System.out and System.err are the objects of type OutputStream


Program to read console input using bufferedReader
import java.io.*;
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new
Socket
InputStreamReader(System.in));
String name = "";
try {
System.out.print("Please enter your name : ");
name = br.readLine(); System.out.println("Hello, " + name + "!");
}
catch(Exception e) {
System.out.println(e);
}
finally {
in.close();
}
}
}
Reading Console input
• In Java 1.0, the only way to perform console input was to use a byte stream.
• For commercial applications, the preferred method of reading console input is to
Socket
use a character-oriented stream.
• This makes your program easier to internationalize and maintain.
• In Java, console input is accomplished by reading from System.in.
• To obtain character-based stream, wrap System.in in a BufferedReader object.
• The BufferedReader class is defined in java.io package.
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in))
▪ System.in refers to an object of type InputStream.
▪ InputStreamReader is the concrete class of Reader class and it converts
bytes to characters.
▪ br is a character-based stream that is linked to the console through System.in.
Reading Console input using bugger reader
Reading Characters
• To read a character from a BufferedReader, use read().
Socket
• Each time read() is called, it reads a character from the input stream and returns it
as an integer value.
• It returns -1 when the end of the stream is encountered.

Reading Strings
• To read a string from BufferedReader, use readLine().
Writing Console Output
Writing console output:
• Console output is accomplished Socket
by print() and println().
• These methods are defined by the PrintStream class (referenced by System.out).
• PrintStream also implements low-level method write().

PrintWriter class
• PrintWriter is a character-based class.
• Using a PrintWriter makes real-world applications easier to internationalize.
PrintWriter pw = new PrintWriter(System.out,true)
• First argument is the OutputStream object

• Second argument is the flushingOn (if true, stream is flushed automatically.)


File Class
• The File class represents a reference to a file or directory.
• The File class has various methods to perform operations.
Socket
• The File class has been defined in the java.io package.
• The File class in java has the following constructors.

File(File parent, String It creates a new File instance from a parent


child) abstract pathname and a child pathname string.
File(String pathname) It creates a new File instance by converting the
given pathname string into an abstract pathname.
File(String parent, String It creates a new File instance from a parent
child) pathname string and a child pathname string.
File(URI uri) It creates a new File instance by converting the
given file: URI into an abstract pathname.
File Class
Modifier Method Description
and Type
static File createTempFile(String ItSocket
creates an empty file in the default temporary-file directory, using the
prefix, String suffix) given prefix and suffix to generate its name.
boolean createNewFile() It atomically creates a new, empty file named by this abstract pathname if
and only if a file with this name does not yet exist.
boolean canWrite() It tests whether the application can modify the file denoted by this
abstract pathname.String[]
boolean canExecute() It tests whether the application can execute the file denoted by this
abstract pathname.
boolean canRead() It tests whether the application can read the file denoted by this abstract
pathname.
boolean isAbsolute() It tests whether this abstract pathname is absolute.
File Class
Modifier and Type Method Description
boolean isDirectory() It tests whether the file denoted by this abstract pathname is a directory.
boolean isFile() It tests whether the file denoted by this abstract pathname is a normal file.
String getName() Socket
It returns the name of the file or directory denoted by this abstract
pathname.
String getParent() It returns the pathname string of this abstract pathname's parent, or null if
this pathname does not name a parent directory.
Path toPath() It returns a java.nio.file.Path object constructed from the this abstract path.
URI toURI() It constructs a file: URI that represents this abstract pathname.
File[] listFiles() It returns an array of abstract pathnames denoting the files in the directory
denoted by this abstract pathname
long getFreeSpace() It returns the number of unallocated bytes in the partition named by this
abstract path name.
String[] list(FilenameFilte It returns an array of strings naming the files and directories in the
r filter) directory denoted by this abstract pathname that satisfy the specified filter.
boolean mkdir() It creates the directory named by this abstract pathname.
Program to find list of files in a directory

import java.io.File; Socket


import java.io.IOException;
public class FileHandlingDemo {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Program Files\\Java\\jdk-21");
String fileNames[]=file.list();
for(String x:fileNames)
System.out.println(x);
}
}
REFERENCES

Text Books:
1. Herbert Schildt, Java: The Complete Reference, 10th Edition, McGraw Hill Education (India) Pvt. Ltd.
2. Herbert Schildt and Dale Skrien, Java Fundamentals - A Comprehensive Introduction, McGraw Hill Education
(India) Pvt. Ltd., 2013.
Reference Books:
1. Jaime Nino and Frederick. A. Hosch, An Introduction to Programming and Object-Oriented Design using Java, John
Wiley & sons, 2013.
2. Timothy Budd, Understanding Object-Oriented Programming with Java, updated Edition, Pearson Education.
3. Y. Daniel Liang, Introduction to Java Programming, Comprehensive Version, 7th Edition, Pearson Education.
4. H.M. Dietel and Dietel, Java – How to Program, 6th Edition, Pearson Education/PHI.
5. Cay Horstmann, Big Java, 4th Edition, John Wiley and Sons Publisher, 2009.

Online Resources:
1. https://docs.oracle.com/javase/tutorial/java/TOC.html
2. https://onlinecourses.nptel.ac.in/noc22_cs47/preview
3. www.javatpoint.com/java-tutorial
Happy journey with
JAVA Programming

You might also like