Posts

PipedInputStream and PipedOutputStream with example

PipedInputStream A piped input stream should be connected  to a piped output stream; the piped  input  stream then provides whatever data bytes  are written to the piped output  stream.  Typically, data is read from a PipedInputStream object by one thread  and data is written  to the corresponding  PipedOutputStream by some  other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread. public PipedInputStream(PipedOutputStream src) throws IOException PipedInputStream() PipedOutputStream A piped output stream can be connected to a piped input stream to create a communications pipe. The piped output stream is the sending end of the pipe. Typically, data is written to a PipedOutputStream object by one thread and data is read from the connected PipedInputStream by some other thread. Attempting to use both objects from a single thread is not recommended as it may deadlock the thread. The...

Scanner class in java

Scanner Java.util.Scanner class provides convenient methods to read data in the form of primitive type as well as string from the keyboard. public Scanner(String source) public Scanner(File source) public Scanner(InputStream source) public int nextInt(int radix) public String nextLine() public float nextFloat() public static void main(String as[])throws IOException{     Scanner sc=new Scanner(System.in);     System.out.println("Enter Roll");     int a=sc.nextInt();     System.out.println(a); } Scanner is also used for Splitting tokens: From above example public class RecorderReadUsingScanner { public static void main(String as[])throws Exception{     BufferedReader b=new BufferedReader(new InputStreamReader(new FileInputStream("d:/Emp.txt")));           Emp e;     while(true)     {        String str=b.readLine(); ...

String Tokenizer

String Tokenizer: Java.util.Tokenizer Class provides the facility to extract token form a token string. Token is sequence of character that is separated by a delimiter by a other tokens. While space as default delimiter. public StringTokenizer(String str, String delim) public StringTokenizer(String str) public boolean hasMoreToken() public String nextToken() String str=”It is a tokend string.”; StringTokenizer st=new StringTokenizer(str); While(st.hasMoreTokens()){ System.out.println(st.nextToken()); } Real Example of StringTokenizer: public class Emp implements Serializable{     String name;     int marks;     String address;     public Emp(){     }     public Emp(String name,int marks, String address)     {         this.name=name;         this.marks=marks;         this.address=...

Read Object and Write Object in file

Read Object and Write Object: public class Emp implements Serializable{     String name;     int marks;     String address;     public Emp(){     }     public Emp(String name,int marks, String address)     {         this.name=name;         this.marks=marks;         this.address=address;     }       public String disp() {         return name+"\t"+marks+"\t"+address;     } } ObjectWriterClass ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("d:/nkser.txt"));     Emp e=new Emp("raju", 300, "patan");     Emp e1=new Emp("raj", 200, "patan");     out.writeObject(e);     out.writeObject(e1);     out.close(); Obj...

File Operation, Read data form one file and write in another file

Read data form one file and write in another file using byte oriented class:-     FileInputStream finput=null;     FileOutputStream foutput=null;     try{      finput=new FileInputStream("d:/nk.txt");     foutput=new FileOutputStream("d:/nk1.txt");     int c;     while((c=finput.read())!=-1){         foutput.write(c);     }     }catch(Exception e){     System.out.print(e);     }     finally{         if(finput!=null)         {                 try {                     finput.close();         ...

Serialization in java

“Serialization means storing a state of a java object by converting it to byte stream.” Typical use of serialization: a.       To persist data for future use b.      To send data to a remote computer c.       To store user session in web application d.      To activate or passivate enterprise java beans “Object state remains persistent in different JVM environment”- by N. K. JHA “The value of any object’s instance variable (fields) define the state of an object. Usually, method calls changed an object’s state.” 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. The serialization interface has no methods or fields. The writeObject method of ObjectOutputStream class is responsible for writing the state of the object for its par...

Input output in java

Input output in java is stream based. A stream represents flow of data or sequence of bytes. Stream provides abstract view of input output. In java two type of input and output streams are provided:-     Byte oriented – represents data in sequence of 8 bits (byte by byte read write)     Char oriented – represents data in sequence of 16 bits. (char by char read write) Super Class of byte oriented stream:- InputStream (read data from a source): -  This abstract class is the superclass of all classes representing an input stream of bytes. OutputStream (write data in a stream) This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Commonly used method of InputStream class: available(): Returns the number of remaining bytes that can be read (or skipped over) from this input stream. public synchronized int available() read(): Reads the next byte of data ...