JAVA Session08
-
Upload
rohit-chaudhary -
Category
Documents
-
view
225 -
download
0
Transcript of JAVA Session08
-
8/8/2019 JAVA Session08
1/20
Slide 1 of 20Session 8Ver. 1.0
Java Programming Language
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 outputWrite 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
Objectives
-
8/8/2019 JAVA Session08
2/20
-
8/8/2019 JAVA Session08
3/20
Slide 3 of 20Session 8Ver. 1.0
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.
-
8/8/2019 JAVA Session08
4/20
Slide 4 of 20Session 8Ver. 1.0
Java Programming Language
Console I/O
Applications interact with the user using console I/O.
Java 2 SDK supports console I/O with three publicvariables 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.
-
8/8/2019 JAVA Session08
5/20
Slide 5 of 20Session 8Ver. 1.0
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 formost primitive types (boolean, char, int, long,
float, and double) and forchar[], Object, and
String.
The print(Object) and println(Object)methods
call the toString() method on the argument.
-
8/8/2019 JAVA Session08
6/20
Slide 6 of 20Session 8Ver. 1.0
Java Programming Language
The application program can use the following methods ofthe java.io package to read from the standard input:
Read characters from the keyboard and convert the raw bytes
into Unicode characters:
InputStreamReader ir=newInputStreamReader(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 ofjava.util package provides
formatted input functionality.
Reading from Standard Input
-
8/8/2019 JAVA Session08
7/20
Slide 7 of 20Session 8Ver. 1.0
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
-
8/8/2019 JAVA Session08
8/20
Slide 8 of 20Session 8Ver. 1.0
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");
-
8/8/2019 JAVA Session08
9/20
Slide 9 of 20Session 8Ver. 1.0
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.
-
8/8/2019 JAVA Session08
10/20
Slide 10 of 20Session 8Ver. 1.0
Java Programming Language
Files and File I/O (Contd.)
The application program can use the following methods ofthe 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 filePrintWriter out = new PrintWriter(new
FileWriter(file));
-
8/8/2019 JAVA Session08
11/20
Slide 11 of 20Session 8Ver. 1.0
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 ofthe 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));
-
8/8/2019 JAVA Session08
12/20
Slide 12 of 20Session 8Ver. 1.0
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);
-
8/8/2019 JAVA Session08
13/20
Slide 13 of 20Session 8Ver. 1.0
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.
-
8/8/2019 JAVA Session08
14/20
Slide 14 of 20Session 8Ver. 1.0
Java Programming Language
The Collections API
Acollection 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 specificordering (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.
-
8/8/2019 JAVA Session08
15/20
Slide 15 of 20Session 8Ver. 1.0
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 afterGenerics code:
ArrayList list = newArrayList();
list.add(0, new Integer(42));
int total = list.get(0).intValue();
-
8/8/2019 JAVA Session08
16/20
Slide 16 of 20Session 8Ver. 1.0
Java Programming Language
Lets see how to use the Collection API and generics in a Java
program.
Demonstration
-
8/8/2019 JAVA Session08
17/20
Slide 17 of 20Session 8Ver. 1.0
Java Programming Language
Iterators
Iteration is the process of retrieving every element in a
collection.
An iterator of a set is unordered.
AListIterator of a List can be scanned forwards
(using the next method) or backwards (using the previousmethod).
List list = new ArrayList();
// add some elements
Iterator elements = list.iterator();
while ( elements.hasNext() ) {
System.out.println(elements.next());
}
-
8/8/2019 JAVA Session08
18/20
Slide 18 of 20Session 8Ver. 1.0
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 loopsIterator disadvantages removed
Iterators are error prone:
Iterator variables occur three times per loop.
This provides the opportunity for code to go wrong.
-
8/8/2019 JAVA Session08
19/20
Slide 19 of 20Session 8Ver. 1.0
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.
-
8/8/2019 JAVA Session08
20/20
Slide 20 of 20Session 8Ver. 1.0
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.