Automation Using Selenium Webdriver

Thursday, 22 December 2016

How to read a file in java with example program



  • We can read java file using buffered reader class in java
  • We need to import java.io.BufferedReader in order to read java text file.
  • Create object of java.io.BufferedReader class by passing new FileReader("C:\\Sample.txt") object to the constructor.
  • In order to read line by line call readLine() method of BufferedReader class which returns current line. Initially first line of java text file
  •  Program #1: Write a java program to read a file line by line using BufferedReader class 

  • package com.instanceofjava.javareadfile;

  • import java.io.BufferedReader;
  • import java.io.FileReader;
  • import java.io.IOException;

  • public class ReadFileBufferedReader {

  •  /**
  •   * 
  •   * @category: How to read java read file line by line using buffered reader
  •   */

  • public static void main(String[] args) {

  •   BufferedReader breader = null;

  •  try {

  •  String CurrentLine;

  •  breader = new BufferedReader(new FileReader("E://Sample.txt"));

  •  while ((CurrentLine = breader.readLine()) != null) {
  •     System.out.println(CurrentLine);
  •  }

  • } catch (IOException e) {
  •    e.printStackTrace();
  • } finally {
  •     try {
  •  if (breader != null)
  •      breader.close();
  •      } catch (IOException ex) {
  •     ex.printStackTrace();
  •      }
  • }

  • }

  • }

  •  Output:
  •   
  • Java open and read file
  • Read file line by line in java
  • Example java program to read a file line by line
  • java read file line by line example


  • Java example program to print message without using System.out.println()

    Is it possible to print message without using system.out.println?




    Yes its possible to print message without using System.out.println("");

    System.out.write("www.corejavawithselenium.blogspot.in\n".getBytes());
     System.out.format("%s", "www.java.com \n")
     PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
       myout.print("www.corejavawithselenium.blogspot.in \n");
    System.err.print("This is custom error message");
    System.console().writer().println("Hai");

    1.System.out.write(Byte [] arg0);

    System.out.write(Byte [] arg0) method The java.io.FilterOutputStream.write(byte[] b) method writes b.length bytes to this output stream.

    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args) throws IOException{

           System.out.write("Hello World".getBytes());
    }
    }

    Output:

    Hello World

    2. PrintStream  java.io.PrintStream.format(String arg0, Object... arg1)

    System.out.format("format",Object obj) by using this method we can format the text and print

    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args){

            System.out.format("%s", "James");
    }

    }

    Output:

    James

    3. FileDescriptor:

    Create PrintStream object by passing FileOutputStream(FileDescriptor.out) object as an argument to the constructor
    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args){

           PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
              myout.print("i love Java");
    }
    }

    Output:

    i love java


    4. System.err.print(""):

    We can use System.err.print("") method to print the message actually its used to print error message
    package com.instanceofjava;

    public static void main(String[] args){

           System.err.print("This is custom error message");
    }
    }

    Output:

    This is custom error message


    5.System.console().writer().println("");

    System.console() returns null if your application is not run in a terminal System.console() provides methods for reading password without echoing characters

    package com.instanceofjava;

    public static void main(String[] args){

           System.err.print("This is custom error message");
    }
    }

    Output:

    This is custom error message Exception in thread "main" java.lang.NullPointerException
        at PrintMessage.main(PrintMessage.java:24)

    Wednesday, 21 December 2016

    How to open notepad using java program



  • Notepad is a text editor from windows operating system. We use notepad for writing text files.
  • We can open a new notepad using java code.
  • By using the concept of running another application by Runtime class in java.
  • By creating object of runtime and calling  exec() method by passing application name.
  • Lets see how to open a notepad using java code
  • OutPut::


  • Program #1: Java example program to open notepad


    1. package interestingJavaprograms;
    2. import java.io.IOException;
    3.  
    4. public class NotepadJava {
    5.  
    6.     /**
    7.      * @ www.corejavawithselenium.blogspot.in
    8.      * @ how to open a new notepad using java program
    9.      */
    10. public static void main(String[] args) {
    11.        
    12.           Runtime rt = Runtime.getRuntime();
    13.           
    14. try {
    15.       rt.exec("notepad");
    16. }
    17.  catch (IOException ex) {
    18.  
    19.  System.out.println(ex);
    20.  
    21. }  
    22.  
    23. }
    24.  
    25. }
  • OUPUT::
  • Tuesday, 20 December 2016

    How to sort arraylist of strings alphabetically java

    1.Basic Java example program to sort arraylist of strings

    1. package com.javasortarraylistofobjects;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Collections;
    5. import java.util.Iterator;
    6.  
    7. public class SortArrayList{
    8.  
    9. public static void main(String[] args) {
    10.   
    11. //create an ArrayList object
    12. ArrayList<String> arrayList = new ArrayList();
    13.        
    14. //Add elements to Arraylist
    15.         
    16. arrayList.add("A"); 
    17. arrayList.add("C");
    18. arrayList.add("D");
    19. arrayList.add("Z");
    20. arrayList.add("F");
    21. arrayList.add("J");
    22. arrayList.add("K");
    23. arrayList.add("M");
    24. arrayList.add("L");
    25. arrayList.add("O");
    26.        
    27.         
    28.  System.out.println("Before sorting ArrayList ...");
    29.  Iterator itr=arrayList.iterator();
    30.         
    31. while (itr.hasNext()) {
    32.  
    33. System.out.println(itr.next());
    34.      
    35. }
    36.  
    37.        
    38.  /*
    39.  To sort an ArrayList object, use Collection.sort method. This is a
    40.   static method. It sorts an ArrayList object's elements into ascending order.
    41. */
    42.   Collections.sort(arrayList);
    43.      
    44.   System.out.println("After sorting ArrayList ...");
    45.        
    46.     
    47.         
    48. Iterator itr1=arrayList.iterator();
    49.         
    50. while (itr1.hasNext()) {

    51. System.out.println(itr1.next());
    52.             
    53. }
    54.     
    55.   
    56. }
    57. }


    OUTPUT::
    1. Before sorting ArrayList ...
    2. A
    3. C
    4. D
    5. Z
    6. F
    7. J
    8. K
    9. M
    10. L
    11. O
    12. After sorting ArrayList ...
    13. A
    14. C
    15. D
    16. F
    17. J
    18. K
    19. L
    20. M
    21. O
    22. Z

    Monday, 19 December 2016

    How to find largest element in an array with index and value using array

    How to find largest element in an array with index and value using array?

    package com.inofjava;
    public class Array {

    public static void main(String[] args) {

    int arr[]={1,120,56,78,87};
    int largest=arr[0];
    int smallest=arr[0];
    int small=0;
    int index=0;

    for(int i=1;i<arr.length;i++){

    if(arr[i]>largest){

    largest=arr[i];
    index=i;

    }
    else if(smallest>arr[i]){

    smallest=arr[i];
    small=i;

    }
    }

    System.out.println(largest);
    System.out.println(index);
    System.out.println(smallest);
    System.out.println(small);

    }

    }


    Output:
    120
    1
    87
    4

    Sunday, 18 December 2016

    Encapsulation in java with example program


    What is meant by encapsulation in java? 
    • Binding the data with its related functionalities known as encapsulation
    • Here data means variables and functionalities means methods.
    • So keeping the variable and related methods in one place.
    • That place is "class". class is the base for encapsulation.
    • Lets see example program on encapsulation, how the variables and methods defined in a class


    What is encapsulation in object oriented programming?
    Basic java example program on data encapsulation:
    1. package com.java;
    2.  
    3. public class EncapsulationDemo {
    4.  
    5.   String name;
    6.   int rno;
    7.   String address;
    8.  
    9. public String getName() {
    10.        return name;
    11.  }
    12.  
    13. public void setName(String name) {
    14.         this.name = name;

    15. public int getRno() {
    16.        return rno;
    17. }
    18.  
    19. public void setRno(int rno) {
    20.      this.rno = rno;
    21. }
    22.  
    23. public String getAddress() {
    24.         return address;
    25. }
    26.  
    27.  public void setAddress(String address) {
    28.     this.address = address;
    29. }
    30.  
    31. public void showInfo(){
    32.  
    33.       System.out.println("Name: "+getName());
    34.       System.out.println("R.No: "+getRno());
    35.       System.out.println("Name: "+getAddress());
    36.  
    37.     }
    38. }

    Class:
    The above example program shows how the variables and methods will be there in a class.
    So class is the base for encapsulation
    class is user defined data type in java means by using class we can structure our own programs.
    Lest see a example program on class.

    1. package com.java;
    2.  
    3. public class ClassDemo {
    4.  
    5.     //variables of a class
    6.    String empName;
    7.    int empId;
    8.    String Designation;
    9.  
    10.  //getter and setter methods for variables 
    11.  //by using these methods we can set the value to the variable and gat the value from the
    12.  //variables
    13.  
    14.  public String getEmpName() {
    15.         return empName;
    16. }
    17.  
    18.  public void setEmpName(String empName) {
    19.       this.empName = empName;
    20. }
    21.  
    22. public int getEmpId() {
    23.        return empId;
    24. }
    25.  
    26. public void setEmpId(int empId) {
    27.         this.empId = empId;
    28. }

    29. public String getDesignation() {
    30.         return Designation;
    31. }
    32.  
    33.  public void setDesignation(String designation) {
    34.         Designation = designation;
    35. }

    36. }

    Object:
    Instance of class is known as object.
    Instance means dynamic memory allocation. So dynamic memory allocation to class is called object.
    By using "new" keyword the object will be created dynamically.
    Class is a template. By using object we will get memory for all variables so that we can use them.
    We can create number of objects for one class based on our requirements.

    Below an example program on class and object.


    1. package com.java; 
    2.  
    3. public class User {
    4.  
    5.    String Name;
    6.     int Id;
    7.     String address;
    8.  
    9.  public String getName() {
    10.      return Name;
    11. }
    12.  
    13. public void setName(String name) {
    14.         Name = name;
    15. }
    16.  
    17. public int getId() {
    18.       return Id;
    19. }
    20.  
    21. public void setId(int id) {
    22.        Id = id;
    23. }
    24.  
    25. public String getAddress() {
    26.         return address;
    27. }
    28.  
    29. public void setAddress(String address) {
    30.         this.address = address;
    31. }
    32.  
    33. public static void main(String [] args){
    34.  
    35.        User obj= new User();
    36.  
    37.         obj.setName("sai");
    38.         obj.setId(1);
    39.         obj.setAddress("xyz");
    40.  
    41.         System.out.println(obj.getName());
    42.         System.out.println(obj.getId());
    43.         System.out.println(obj.getAddress());
    44.  
    45.     }
    46. }

    Output:

    1. sai
    2. 1
    3. xyz