Enterprise Application Development 1
By Dr. Randima Dinalankara
[email protected] Main topics - Today
• Language Fundamentals
• Object Oriented Programming
• Interface & Packaging
• GUI and Event handling
• Exception Handling
• Database Programming and Reporting
• String & IO handling
• Generics and Collections
• Lambda & Stream
• Multithreading Programming
• Design Patterns
String
String
• String Class in Java
• String represents a sequence of charactors.
• Java Strings is a collection of two classes
– string
– stringBuffer
• String can be declared and created as follows,
– String name = new String(“Enterprice App. Development”);
▪ toLowerCase() Converts the string s1 to all lowercase
▪ toUpperCase() Converts the string s1 to all uppercase
▪ replace(“x”,”y”) Replace all appearance of x with y
▪ trim() Remove white spaces at the beginning and end of the
strings1
▪ equals(s2) Returns ‘true’ if s1 is equal to s2
▪ equalsIgnoreCase(s2) Returns ‘true’ if s1=s2, ignoring the case of
characters
▪ charAt(n) Gives nth character of s1
▪ concat(s2) concatenates s1 and s2
String class methods (cont.)
• s1.substring(n) Gives substring starting from nth character
• s1.substring(n,m) Gives substring starting from nth character up to
(m-1)th character
• String.ValueOf(<variable>) Converts the parameter value to string
representation
• p.toString() - Creates a string representation of the object p
• s1.indexOf(‘x’) - Gives the position of the first occurrence of ‘x’ in
the string s1
• s1.indexOf(‘x’,n) - Gives the position of ‘x’ that occurs after the nth
position in the string s1
Example
String sentence1 = new String("Hello World ");
String sentence2 = new String("Java");
System.out.println(sentence1);
System.out.println(sentence2);
System.out.println(sentence1+"->"+sentence1.toLowerCase());
System.out.println(sentence1+"->"+sentence1.toUpperCase());
System.out.println(sentence1+"->"+sentence1.replace('e','a'));
System.out.println(sentence1.trim()+"->"+sentence1); Hello World
Java
String sentence3 = sentence1.concat(sentence2); Hello World ->hello world
Hello World ->HELLO WORLD
System.out.println(sentence3);
Hello World ->Hallo World
System.out.println(sentence1.substring(6)); Hello World->Hello World
System.out.println(sentence1.substring(2,4)); Hello World Java
System.out.println("index of o "+sentence1.indexOf('o')); World
ll
System.out.println("index of o "+sentence1.indexOf('o',5));
index of o 4
index of o 7
String Buffer
• StringBuffer is a peer class of String.
• String creates strings with fixed-length.
• StringBuffer creates strings of flexible length that can be
modified in terms of both length and content.
String Buffer methods
▪ setCharAt(n,’x’) modify nth character to x
▪ append(s2) appends the string s2 to at s1 at the end
▪ Insert(n,s2) insert the string s2 at the position nth of string s1
▪ setLength(n) sets the length of the string s1 to n
Example
StringBuffer sentence1 = new StringBuffer("www.abc.com/create/user");
System.out.println(sentence1);
sentence1.insert(7,">>Added<<");
System.out.println(sentence1);
sentence1.append(">>Added to the End<<<");
System.out.println(sentence1);
www.abc.com/create/user
www.abc>>Added<<.com/create/user
www.abc>>Added<<.com/create/user>>Added to the End<<<
I/O handling
IO handling
• Variables loose data once programme ends.
• Difficult to handle large number of variables as well.
Because of that
• Store the content of variables to a secondary storage.
• Secondary storages create files to store data.
IO handling (cont.)
• A file is a
– collection of perticular records
– located in the secondary storage
– What is a record?
• A collection of several fields, where a field consists of charactors.
• A charactor is a unicode with two Bytes.
– A File can be
• Created, manipulated, updated, etc in Java.
IO handling (cont.)
• Java provides classes for IO operations
– read class
– write class
• Reading or Writing object is known as Object Serialization.
What is a stream?
• It is an ordered sequence of data.
• Stream represents an interface between program and I/O device.
• Input Stream - data come in to the program
• Output Stream - data go out from the program
– Direction is decided based on requirement
• Stream has source and destination.
Stream categories based on data types
• Two groups
– Byte stream classes
– Charactor stream classes
• Byte stream classes
– Unidirectional (only one direction at a time)
– Write data in bytes.
• Charactor stream classes
– Read/Write 16-bit unicode charactor.
Syntax of creating a streaming object
<stream class> <obj> = new <stream class>(source/destination);
• Source/Destination is an Input Output Device locaiton
– File in the hard disk
– Printer/Scanner
• Refer www.javatpoint.com
Create and write a simple file
import java.io.FileOutputStream;
public class FileOperations1 {
public static void main(String[] args) {
System.out.println("File operations");
//write a single charactor in an integer format to a file
try{
FileOutputStream outputFile = new FileOutputStream("test.txt");
outputFile.write(70);
outputFile.close();
System.out.println("File operation is successful...");
}catch(Exception e){
System.out.println(e);
}
}
}
Output stream
Image is from www.javapoint.com/java-io
Input stream
Image is from www.javapoint.com/java-io
Create and write a simple file 2
import java.io.FileOutputStream;
public class FileOperations1 {
public static void main(String[] args) {
System.out.println("File operations");
//write a string to a file
try{
FileOutputStream outputFile = new FileOutputStream("test.txt");
String sentence = "This is a test file";
byte byteSentence[] = sentence.getBytes();
outputFile.write(byteSentence);
outputFile.close();
System.out.println("File operation is successful...");
}catch(Exception e){
System.out.println(e);
}
}
}
Read a file
import java.io.FileInputStream;
public class FileOperations2 {
public static void main(String[] args) {
System.out.println("File operations");
//read a data to a file
try{
FileInputStream inputFile = new FileInputStream("test.txt");
int fileData = inputFile.read();
System.out.println((char)fileData);
inputFile.close();
System.out.println("File operation is successful...");
}catch(Exception e){
System.out.println(e);
}
}
}
Read the entire file content
try{
FileInputStream inputFile = new FileInputStream("test.txt");
//read entire file
int charactor=0;
while( (charactor=inputFile.read()) != -1){
System.out.print((char)charactor);
}
inputFile.close();
System.out.println("File operation is successful...");
}catch(Exception e){
System.out.println(e); import java.io.FileInputStream
}
Practice Problem
• Consider the email address of a person is entered to the text field
called txtEmail. Write a statement to convert the content of
txtEmail to lower case letters and store it to a String variable
email.
• Write a program to read a file called ‘new_members.csv’, which
holds id, first name, last name, and mobile number of the
members, and display the content in the console. The above fields
are separated by commas. A record should be displayed as
follows
– Lasith Perera’s mobile is 0712345678.