Tutorialspoint

Java.io.PushbackReader.mark() Method

package com.tutorialspoint;

import java.io.*;

public class PushbackReaderDemo {
   public static void main(String[] args) {      
      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      // create a new PushBack reader based on our string reader
      PushbackReader pr = new PushbackReader(sr, 20);
      
      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.print("" + c);
         }

         // change line
         System.out.println();

         // mark the current position results in exception
         pr.mark(5);

         // close the stream
         pr.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Java.io.ObjectOutputStream.PutField.put() Method

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo implements Serializable {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());
         oout.flush();
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static short var = 5;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Short.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         short d = 87;
         var = fields.get("var", d);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Java.io.ObjectOutputStream.PutField.put() Method

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo implements Serializable {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());
         oout.flush();
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static short var = 5;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Short.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         short d = 87;
         var = fields.get("var", d);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Java.io.ObjectOutputStream.PutField.put() Method

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo implements Serializable {
   public static void main(String[] args) {
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(new Example());
         oout.flush();
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var of a
         System.out.println("" + a.var);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static short var = 5;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Short.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at var
         ObjectInputStream.GetField fields = in.readFields();

         // get var
         short d = 87;
         var = fields.get("var", d);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Java two Strings lexicographically

public class Tester{
   public static void main(String args[]){
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;

      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );
      System.out.println( str.compareTo(objStr.toString()));
   }
}

Java CopyOnWriteArrayList Class

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Tester {

   public static void main(String args[]) {
      // create an array list
      CopyOnWriteArrayList<String> al = new CopyOnWriteArrayList();
      System.out.println("Initial size of al: " + al.size());

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());

      // display the array list
      System.out.println("Contents of al: " + al);

      // Remove elements from the array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: " + al.size());
      System.out.println("Contents of al: " + al);

      try{
         Iterator<String> iterator = al.iterator();
         while(iterator.hasNext()){
            iterator.remove();
         }
      }catch(UnsupportedOperationException e){
         System.out.println("Method not supported:");
      }
      System.out.println("Size of al: " + al.size());
   }
}

Java CopyOnWriteArraySet Class

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;
public class Tester {

   public static void main(String args[]) {
      // create an array list
      CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet();
      System.out.println("Initial size of set: " + set.size());
      int count[] = {34, 22,10,60,30,22};
      // add elements to the array list
      for(int i = 0; i < 5; i++) {
         set.add(count[i]);
      }
      System.out.println("Size of set after additions: " + set.size());

      // display the set
      System.out.println("Contents of set: " + set);

      // Remove elements from the array list
      set.remove(10);
      System.out.println("Size of set after deletion: " + set.size());
      System.out.println("Contents of set: " + set);

      try{
         Iterator<Integer> iterator = set.iterator();
         while(iterator.hasNext()){
            iterator.remove();
         }
      }catch(UnsupportedOperationException e){
         System.out.println("Method not supported:");
      }
      System.out.println("Size of set: " + set.size());
   }
}

Java.io.InputStreamReader.close() Method

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      InputStreamReader isr = null;
      int i;
      char c;
      
      try {
         // new input stream reader is created 
         fis = new FileInputStream("C:/test.txt");
         isr = new InputStreamReader(fis);
         
         // input stream reader is closed
         isr.close();
         System.out.print("close() invoked");
         
         // read() called after closed method
         i = isr.read();
         c = (char)i;
         System.out.println(c);
      
      } catch (Exception e) {
         // print error
         System.out.print("The stream is already closed");
      } finally {
         // closes the stream and releases resources associated
         if(fis!=null)
            fis.close();
         if(isr!=null)
            isr.close();
      }   
   }
}

Java CountDownLatch

import java.util.concurrent.CountDownLatch;

public class Tester {
   public static void main(String args[]) {
      //Set the counter to 2 being two applications   
      CountDownLatch countDownLatch = new CountDownLatch(4);
   
      Thread app1 = new Thread(new Application("App1",  countDownLatch));
      Thread app2 = new Thread(new Application("App2",  countDownLatch));   
      Thread app3 = new Thread(new Application("App3",  countDownLatch));
      Thread app4 = new Thread(new Application("App4",  countDownLatch));    

      // initialize applications
      app1.start(); 
      app2.start(); 
      app3.start(); 
      app4.start(); 
      
      try{
         //wait until countDownLatch reduces to 0.
         countDownLatch.await();  
         //As all applications are up, print the message
         System.out.println("All applications are up and running.");
      } catch(InterruptedException e){
         System.out.println(e.getMessage());
      }     
   }
}

class Application implements Runnable {
   private String name;
   private CountDownLatch countDownLatch;

   public Application(String name, CountDownLatch countDownLatch){
      this.name = name;
      this.countDownLatch = countDownLatch;
   }

   public void run() {
      try {
         System.out.println(name + " started. ");
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         System.out.println(e.getMessage());
      }
      System.out.println( name + " is Up and running.");
      //reduce the count by 1
      countDownLatch.countDown(); 
   } 
}

Java Loose Coupling

import java.io.IOException;

public class Tester {
   public static void main(String args[]) throws IOException {
      Show b = new B();
      Show c = new C();

      A a = new A(b);    
      //a.display() will print A and B     
      a.display();

      A a1 = new A(c);
      //a.display() will print A and C     
      a1.display();
   }
}

interface Show {
   public void display();
}

class A {
   Show s;
   public A(Show s){
      //s is loosely coupled to A 
      this.s = s;
   }
   public void display(){
      System.out.println("A");
      s.display();
   }
}

class B implements Show { 
   public B(){}
   public void display(){
      System.out.println("B");
   }
}

class C implements Show { 
   public C(){}
   public void display(){
      System.out.println("C");
   }
}

1 2 3 4 5 6 7 ... 105 Next
Advertisements
Loading...