0% found this document useful (0 votes)
18 views23 pages

UNIT 4 Class Notes

sfbsbsdbsb

Uploaded by

jothishwar007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

UNIT 4 Class Notes

sfbsbsdbsb

Uploaded by

jothishwar007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT 4

ADVANCED STRING PROCESSING, OBJECT


SERIALIZATION, AND I/O TECHNIQUES
STRING TOKENIZER
• StringTokenizer class in Java is used to break a string into tokens

• A token is returned by taking a substring of the string that was used to create
the StringTokenizer object. It provides the first step in the parsing process
often called lexer or scanner.

• StringTokenizer in Java is a utility class that breaks a string into pieces or


multiple strings called “tokens”. For example, strings separated by space and
tab characters are tokens. These tokens are separated with the help of a
delimiter.
• A Delimiter is a group of characters that separate tokens. Each
character in the delimiter is considered a valid delimiter. For example,
comma, semicolons, and colon are sets of delimiters

• By default, space, tab, newline, and carriage return are used to


separate the strings. But we can also use any other characters to
separate tokens.
StringTokenizer Class Declaration

• If you want to use StringTokenizer, you must import java.util package


or at least StringTokenizer class into your application.

• public class StringTokenizer extends Object implements


Enumeration<Object>
Constructors of StringTokenizer Class:
• StringTokenizer(String str): This constructor is used to construct a string
tokenizer for the specified string str. The parameter str is a string that will be
tokenized. The default delimiters are used.

• StringTokenizer(String str, String delim): This constructor is used to construct a


string tokenizer for the specified string and delimiter. The parameter delim is a string
that represents delimiters to separate the tokens

• StringTokenizer(String str, String delim, boolean delimAsToken): This form of


constructor constructs StringTokenizer with specified string, delimiter, and
returnValue.
StringTokenizer Class
• StringTokenizer class in Java is useful to public static void main(String[] args) {

separate tokens. These tokens are then stored in String sentence = "A blessing on disguise";

the StringTokenizer object from where they can


StringTokenizer tokenizer = new
be retrieved.
StringTokenizer(sentence);
• StringTokenizer st = new StringTokenizer(str,
System.out.println("Tokens in the sentence:");
"delimiter");
while (tokenizer.hasMoreTokens()) {
Example
System.out.println(tokenizer.nextToken());
import java.util.StringTokenizer;
} }}
Public class Main {
• For example, to break the string “Hello Scientech easy” whenever a
comma is found, we can write as follows:

• StringTokenizer st = new StringTokenizer("Hello Scientech Easy", ",");

• Similarly, to break a string whenever a comma, or semi-colon, or both are


found, we can specify delimiters as follows:

• String delimiters = ",;";


Methods of StringTokenizer in Java

1. int countTokens(): This method counts and returns the number of tokens available
in the StringTokenizer object.
2. boolean hasMoreTokens(): This method checks if there are more tokens available
in the StringTokenizer object or not. If the next token is available, it will return true.
3. String nextToken(): This method returns the next token from the string tokenizer
object.
4. String nextToken(String delim): It returns the next token from the string
tokenizer object based on the delimiter.
5. boolean hasMoreElements(): It returns the same value as the hasMoreTokens
method.
6. Object nextElement(): It returns the same value as the nextToken method, but its
return type is Object.
Java StringTokenizer Example Programs
import java.util.StringTokenizer; // Now retrieve tokens from st and display them.
public class StringTokens { System.out.println("Tokens are: ");
public static void main(String[] args) while(st.hasMoreTokens()){
{ String tokens = st.nextToken();
// Take a string. System.out.println(tokens);
String str = "He is a gentle man"; }
}
// Take a string for delimiters. }
String delimiters = " ,"; // Here, delimiters are a space and
a comma. Output:

// Create an object of string tokenizer and break into tokens. Number of tokens: 5
Here, delimiters are a space and a comma.
Tokens are:
StringTokenizer st = new StringTokenizer(str, delimiters);
He
is
// Counts the number of tokens available in string tokenizer
object. a
int counts = st.countTokens(); gentle
System.out.println("Number of tokens: " +counts); man
.
Split() method:

• String[] s = str.split(delimiters);
Tokenizing and Formatting
String tokenization and formatting can be used to process and System.out.println(formattedToken);
reformat tokens extracted from a string. Example: Tokenizing
and Formatting a Sentence }
Input String: "hello geeks, how are you doing today?" }
Objective: Tokenize the string and format each token by private static String formatToken(String token) {
capitalizing the first letter of each word.
if (token.length() == 0) return token;
import java.util.StringTokenizer;
return token.substring(0, 1).toUpperCase() +
public class TokenizingAndFormatting { token.substring(1).toLowerCase();
public static void main(String[] args) { }
String str = "hello geeks, how are you doing today?"; }
StringTokenizer tokenizer = new StringTokenizer(str,
" ,?");

while (tokenizer.hasMoreTokens()) {

String token = tokenizer.nextToken();

String formattedToken = formatToken(token);


Output:

Hello
Geeks
How
Are
You
Doing
Today
Locating Data via Pattern Matching, Tokenizing

• Locating data via pattern matching and tokenizing involves identifying


specific patterns in a string and then breaking the string down into
tokens based on those patterns. This is useful in various text
processing tasks, such as extracting information from text, validating
inputs, and data transformation
• SNIPPET
public class PatternMatchingAndTokenizing { System.out.println(tokenizer.nextToken());
public static void main(String[] args) { }
String text = "Contact us at }
support@example.com or sales@example.org. private static ArrayList<String>
Visit our website at https://example.com."; locateEmails(String text) {
ArrayList<String> emails = ArrayList<String> emails = new
locateEmails(text); ArrayList<>();
Pattern emailPattern =
System.out.println("Located Emails:"); Pattern.compile("[a-zA-Z0-9._%+-]+@[a-zA-
Z0-9.-]+\\.[a-zA Z]{2,}");
for (String email : emails) {
Matcher matcher =
System.out.println(email); emailPattern.matcher(text);
} while (matcher.find()) {
System.out.println("\nTokens:"); emails.add(matcher.group());
StringTokenizer tokenizer = new }
StringTokenizer(text, " ,.@");
while (tokenizer.hasMoreTokens()) {
OBJECT SERIALIZATION
• Serialization in Java is the concept of representing an object’s state as a byte stream. The byte stream

has all the information about the object.

• Deserialization is the exact opposite process of serialization where the byte data type stream is

• You can serialize an object only by implementing the serializable interface

• All the fields of a class must be serializable; otherwise, use the transient keyword (more about it later)

• The child class doesn’t have to implement the Serializable interface, if the parent class does

• The serialization process only saves non-static data members, but not static or transient data members

By default, the String and all wrapper classes implement the Serializable interface

converted back to an object in the memory


ObjectOutputStream/ ObjectInputStream.

• Serialization is done using ObjectOutputStream. Deserialization is the


reverse process where the byte stream is used to recreate the actual
Java object in memory. This mechanism is used to persist the object.
Deserialization is done using ObjectInputStream.
Writeobj()/readobj()

• public final void writeObject(Object o) throws IO ExceptionSyntax


for the readObject() method:

• public final Object readObject() throws IOException,


ClassNotFoundException
Advanced Input & Output (I/O)

JDK 1.4 introduced the so-called New I/O (or NIO), in java.nio package and its
auxiliary packages, to support high performance and intensive I/O operations.
NIO is meant to complement the existing Standard I/O (in java.io package), not as
a replacement.
Java NIO (New Input/Output) is a high-performance networking and file handling
API introduced in JDK 4. It serves as an alternative to the standard Java I/O
system, offering advanced features and a different programming model.
• Physical I/O operation is thousands times slower than memory access.
Hence, a chunk of data is often cache or buffer to improve the
throughput. As illustrated from the above diagram, many layers of
cache exist between your Java application and physical disk.

You might also like