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

JAVA Session08

This document provides an overview of topics to be covered in a Java programming language session, including: - Writing programs that use command-line arguments and system properties - Writing programs that can read from standard input and use formatted I/O - Writing programs that can create, read, and write files - Describing Java's basic collection hierarchy and writing programs using sets, lists, and generic collections - Iterating over collections

Uploaded by

Rohit Chaudhary
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

JAVA Session08

This document provides an overview of topics to be covered in a Java programming language session, including: - Writing programs that use command-line arguments and system properties - Writing programs that can read from standard input and use formatted I/O - Writing programs that can create, read, and write files - Describing Java's basic collection hierarchy and writing programs using sets, lists, and generic collections - Iterating over collections

Uploaded by

Rohit Chaudhary
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 20

Java Programming Language

Objectives

In this session, you will learn to:


Write a program that uses command-line arguments and
system properties
Write a program that reads from standard input
Describe the C-type formatted input and output
Write a program that can create, read, and write files
Describe the basic hierarchy of collections
Write a program that uses sets and lists
Write a program to iterate over a collection
Write a program that uses generic collections

Ver. 1.0 Session 8 Slide 1 of 20


Java Programming Language
Command-Line Arguments

Command-line arguments are the parameters passed to a


Java application at run time.
Each command-line argument is placed in the args array
that is passed to the static main method. For example:
public static void main(String[] args)

Ver. 1.0 Session 8 Slide 2 of 20


Java Programming Language
System Properties

System properties are a feature that replaces the concept of


environment variables (which are platform-specific).
System properties include information about the current
user, the current version of the Java runtime, and the
character used to separate components of a file path name.
The System.getProperties() method returns a
Properties object.
The System.getProperty(String) method returns a
String representing the value of the named property.
The System.getProperty(String, String)
method enables you to supply a default string value (second
parameter), which is returned if the named property does not
exist.

Ver. 1.0 Session 8 Slide 3 of 20


Java Programming Language
Console I/O

Applications interact with the user using console I/O.


Java 2 SDK supports console I/O with three public
variables in the java.lang.System class:
The variable System.out enables you to write to standard
output. It is an object of type PrintStream.
The variable System.in enables you to read from standard
input. It is an object of type InputStream.
The variable System.err enables you to write to standard
error. It is an object of type PrintStream.

Ver. 1.0 Session 8 Slide 4 of 20


Java Programming Language
Writing to Standard Output

The println() method print the argument and a newline


character (\n).
The print() method print the argument without a newline
character.
The print() and println() methods are overloaded for
most primitive types (boolean, char, int, long,
float, and double) and for char[], Object, and
String.
The print(Object) and println(Object) methods
call the toString() method on the argument.

Ver. 1.0 Session 8 Slide 5 of 20


Java Programming Language
Reading from Standard Input

The application program can use the following methods of


the java.io package to read from the standard input:
Read characters from the keyboard and convert the raw bytes
into Unicode characters:
InputStreamReader ir=new
InputStreamReader(system.in);
Create a buffered reader to read each line from the keyboard:
BufferedReader in = new BufferedReader(ir);

The BufferedReader(in) provides a readLine() method


to read from standard input one line at a time:
s=in.readLine();
The Scanner class of java.util package provides
formatted input functionality.

Ver. 1.0 Session 8 Slide 6 of 20


Java Programming Language
Files and File I/O

The java.io package enables you to do the following:


Create File objects
Manipulate File objects
Read and write to file streams

Ver. 1.0 Session 8 Slide 7 of 20


Java Programming Language
Files and File I/O (Contd.)

Creating a new File Object:


File myFile;
The File class provides several utilities:
myFile = new File("myfile.txt");
myFile = new File("MyDocs", "myfile.txt");
Directories are treated just like files in Java; the File class
supports methods for retrieving an array of files in the
directory, as follows:
File myDir = new File("MyDocs");
myFile = new File(myDir, "myfile.txt");

Ver. 1.0 Session 8 Slide 8 of 20


Java Programming Language
Files and File I/O (Contd.)

For file input:


Use the FileReader class to read characters.
Use the BufferedReader class to use the readLine()
method.
For file output:
Use the FileWriter class to write characters.
Use the PrintWriter class to use the print() and
println() methods.

Ver. 1.0 Session 8 Slide 9 of 20


Java Programming Language
Files and File I/O (Contd.)

The application program can use the following methods of


the java.io package to read input lines from the keyboard
and write each line to a file:
Create file
File file = new File(args[0]);
Create a buffered reader to read each line from the keyboard
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
Create a print writer on this file
PrintWriter out = new PrintWriter(new
FileWriter(file));

Ver. 1.0 Session 8 Slide 10 of 20


Java Programming Language
Files and File I/O (Contd.)

Read each line from the input stream and print to a file one line
at a time:
s = in.readLine();
out.println(s);
The application program can use the following methods of
the java.io package to read from a text file and display
each line on the standard output.
Create file:
File file = new File(args[0]);
Create a buffered reader to read each line from the keyboard:
BufferedReader in = new BufferedReader(new
FileReader(file));

Ver. 1.0 Session 8 Slide 11 of 20


Java Programming Language
Files and File I/O (Contd.)

Read each line from the file and displays it on the standard
output:
s = in.readLine();
System.out.println(s);

Ver. 1.0 Session 8 Slide 12 of 20


Java Programming Language
Demonstration

Lets see how to read data from a file and display the output on
the standard output device. This demo also shows how to run a
program with user provided command line arguments.

Ver. 1.0 Session 8 Slide 13 of 20


Java Programming Language
The Collections API

A collection is a single object representing a group of


objects known as its elements.
The Collections API contains interfaces that group objects
as one of the following:
Collection: A group of objects called elements; any specific
ordering (or lack of) and allowance of duplicates is specified by
each implementation.
Set: An unordered collection; no duplicates are permitted.
List: An ordered collection; duplicates are permitted.

Ver. 1.0 Session 8 Slide 14 of 20


Java Programming Language
Generics

Generics are described as follows:


Provides compile-time type safety
Eliminates the need for casts
Example of before Generics code:
ArrayList list = new ArrayList();
list.add(0, new Integer(42));
int total =
((Integer)list.get(0)).intValue();
Example of after Generics code:
ArrayList<Integer> list = new
ArrayList<Integer>();
list.add(0, new Integer(42));
int total = list.get(0).intValue();

Ver. 1.0 Session 8 Slide 15 of 20


Java Programming Language
Demonstration

Lets see how to use the Collection API and generics in a Java
program.

Ver. 1.0 Session 8 Slide 16 of 20


Java Programming Language
Iterators

Iteration is the process of retrieving every element in a


collection.
An iterator of a set is unordered.
A ListIterator of a List can be scanned forwards
(using the next method) or backwards (using the previous
method).
List list = new ArrayList();
// add some elements
Iterator elements = list.iterator();
while ( elements.hasNext() ) {
System.out.println(elements.next());
}

Ver. 1.0 Session 8 Slide 17 of 20


Java Programming Language
Enhanced for Loop

The enhanced for loop has the following characteristics:


Simplified iteration over collections
Much shorter, clearer, and safer
Effective for arrays
Simpler when using nested loops
Iterator disadvantages removed
Iterators are error prone:
Iterator variables occur three times per loop.
This provides the opportunity for code to go wrong.

Ver. 1.0 Session 8 Slide 18 of 20


Java Programming Language
Summary

In this session, you learned that:


A program can be parameterized by command-line arguments
and system properties.
Applications interaction with the user is accomplished using text
input and output to the console.
The Scanner class provides formatted input functionality. It is a
part of the java.util package.
Java.io package enables you to create file objects, manipulate
them, and read and write to the file streams.
The J2SE platform supports file input in two forms:
The FileReader class to read characters.
The BufferedReader class to use the readLine method.
The J2SE platform supports file output in two forms:
The FileWriter class to write characters.
The PrintWriter class to use the print and println methods.

Ver. 1.0 Session 8 Slide 19 of 20


Java Programming Language
Summary (Contd.)

A collection is a single object representing a group of objects.


These objects are known as elements. There are two types of
collections:
Set: It is an unordered collection, where no duplicates are
permitted.
List: It is an ordered collection, where duplicates are permitted.
The Iterator interface enables you to scan forward through
any collection. The enhanced for loop can be used to iterate
through a collection.
Generics provide compile-time type safety and eliminate the
need for casts.

Ver. 1.0 Session 8 Slide 20 of 20

You might also like