Sie sind auf Seite 1von 29

API Contents

BY: Akhilesh Kumar BY: Rahul

Overview

String StringBuffer StringBuilder Wrapper Classes Streams Categories of Streams File Character Stream Hierarchy Byte Stream Hierarchy Serialization Date, Number, Currencies
Slide 2 of 30

String

Strings are objects. Just like other objects, we can create an instance of a String using new keyword, String s = Java; String s = new String(Java); Strings are Immutable Objects

Slide 3 of 30

String

Slide 4 of 30

String - Abandoned

Slide 5 of 30

String

String s1 = "spring ";


String s2 = s1 + "summer "; s1.concat("fall "); s2.concat(s1); s1 += "winter ";

System.out.println(s1 + " " + s2);

Output :
spring winter spring summer

No of reference Variable and String objects?


2,8

Slide 6 of 30

Strings and Memory


String constant pool memory Normal (non-pool) memory String s = "abc"; // creates one String object and one reference variable String s = new String("abc"); // creates two objects, and one reference variable

Slide 7 of 30

String - Methods

public char charAt(int index) public String concat(String s) public boolean equalsIgnoreCase(String s) public int length() public String replace(char old, char new) public String substring(int begin) public String substring(int begin, int end) public String toLowerCase() public String toUpperCase() public String trim()

Slide 8 of 30

StringBuffer

Used when we have to make a lot of modifications to strings of characters All methods are synchronized StringBuffer sb = new StringBuffer(Welcome ");
sb.append( to Java"); System.out.println("sb = " + sb); // Welcome to Java

Slide 9 of 30

StringBuilder

Added in Java 5 Similar to StringBuffer Is not thread safe methods are not synchronized Works faster than StringBuffer

StringBuilder sb = new StringBuilder("abc");


sb.append("def").reverse().insert(3, "---"); System.out.println( sb ); // output is "fed---cba"

Slide 10 of 30

Common Methods

public synchronized StringBuffer append(String s) public synchronized StringBuffer append(float f) // all primitive args public StringBuilder delete(int start, int end)

public StringBuilder insert(int offset, String s) pubic StringBuilder insert(int offset, int i) // second all primitive args public synchronized StringBuffer reverse()
public String toString()
Slide 11 of 30

Wrapper Classes

Slide 12 of 30

Wrapper Classes

Common methods of Wrapper Class

xxxValue()
byteValue(), shortValue(), longValue()

parseXxx()
parseInt(), parseByte(), parseLong()

valueOf()
Integer.valueOf(101011,2); //2-base

toString() toXxxString()
Integer.toHexString(254) Long.toOctalString(254)

Slide 13 of 30

Streams

Sequence of bytes traveling from a source to a destination over a communication path. Input stream receives data from a source Output stream sends data to a destination from the program Standard input/output stream in Java is represented by three members of the system class : in, out and err

Slide 14 of 30

Categories of Stream

Character Streams
these provide a way to handle character oriented input/output operations they make use of Unicode and can be internationalized

Byte Streams
these provide a way to handle byte oriented input/output operations InputStream and OutputStream classes are at the top of their hierarchy

Slide 15 of 30

I/O Classes

File FileReader BufferedReader FileWriter BufferedWriter PrintWriter Console DataInputStream DataOutputStream FileInputStream FileOutputStream ObjectInputStream ObjectOutputStream Serializable

Slide 16 of 30

File

Represents the pathname of a file or directory in the host file system Used to obtain or manipulate the information associated with a disk file, such as permissions, time, date, directory path etc An object of File class provides a handle to a file or directory and can be used to create, rename or delete the entry The File class isn't used to actually read or write data

Slide 17 of 30

File - Methods

canRead() exists() isFile() isDirectory() getAbsolutePath() getName() getPath() getParent() length() : returns length of file in bytes as long lastModified() mkdir() list() : obtain listings of directory contents
Slide 18 of 30

New File using Class


Objects of type File are used to represent the actual files (but not the data in the files) or directories that exist on a computer's physical disk. import java.io.*;
class Writer1 { public static void main(String [] args) { File file = new File("fileWrite1.txt"); } }

import java.io.*;
class Writer1 { public static void main(String [] args) { try { boolean newFile = false; File file = new File ("fileWrite1.txt"); System.out.println(file.exists()); // look for a real file newFile = file.createNewFile(); // maybe create a file! System.out.println(newFile); // already there? System.out.println(file.exists()); // look again } catch(IOException e) { } } }

Slide 19 of 30

Character Streams

Reader and Writer classes are designed for character streams Reader is an input character stream that reads a sequence of Unicode characters Writer is an output character stream that writes a sequence of Unicode characters

Slide 20 of 30

FileReader

This class is used to read character files. read() methods are fairly low-level, allowing you to read single characters, the whole stream of characters, or a fixed number of characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders, which improve performance and provide more convenient ways to work with the data.

Slide 21 of 30

BufferedReader

More efficient than FileReader and easier to use. Compared to FileReaders, BufferedReaders read relatively large chunks of data from a file at once, and keep this data in a buffer. It read next character or line of data from the buffer, which minimizes the number of times that timeintensive, file read operations are performed. Provides more convenient methods such as readLine(), that allow to get the next line of characters from a file.

Slide 22 of 30

FileWriter

Used to write to character files. Its write() methods used to write character(s) or Strings to a file. FileWriters are usually wrapped by higher-level Writer objects such as BufferedWriters or PrintWriters, which provide better performance and higher-level, more flexible methods to write data.

Slide 23 of 30

BufferedWriter

More efficient than FileWriter and easier to use Compared to FileWriters, BufferedWriters write relatively large chunks of data to a file at once. minimizing the number of times that slow, file writing operations are performed. In addition, the BufferedWriter class provides a newLine() method that makes it easy to create platform-specific line separators automatically.

Slide 24 of 30

PrintWriter

This class has been enhanced significantly in Java 5. Because of newly created methods and constructors (like building a PrintWriter with a File or a String) We can use PrintWriter in places where previously needed a Writer to be wrapped with a FileWriter and/or a BufferedWriter. New methods like format(), printf(), and append() make PrintWriters very flexible and powerful.

Slide 25 of 30

Console

Addition in Java 6 Convenience class provides methods to read input from the console and write formatted output to the console

Methods to access the character-based console device, if any, associated with the current Java virtual machine.

Slide 26 of 30

Serialization
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable

Object Serialization
Allows an object to be transformed into a sequence of bytes that can be later re-created into an original object After deserialization, the object has the same state as it had when it was serialized(barring any data members that are not serialized) For a object to be serialized, the class must implement
Slide 27 of 30

Dates, Numbers, Currencies


java.util.Date java.util.Calendar java.text.DateFormat java.text.NumberFormat java.util.Locale

Slide 28 of 30

Summary

String StringBuffer StringBuilder Wrapper Classes Streams Categories of Streams File Character Stream Hierarchy Byte Stream Hierarchy Serialization Date, Number, Currencies

Slide 29 of 30

Das könnte Ihnen auch gefallen