HomeUncategorized

Packages, Handling Files and Collections Framework (Selenium 4 – Session 13)


Packages

Packages are created to group related classes/interfaces/other files.

  • We generally group things to organize them better for locating them easily.
  • Package creation – Create a new Java project say Facebook and group the Classes under various packages – view here
  • Demonstrate – Accessing instances variables and methods from other class which is under the same package
  • Demonstrate –  Importing the Classes in the other packages while accessing the instance variables and methods created in the Classes which are under other packages
  • Demonstrate – default and protected (within package and outside package)
  • Demonstrate – Using * in the import statements to import all the classes in the package instead of importing a single class every time

Handling Files

  • File is a predefined Class in Java
  • Using File Class represent a file in Java, which is available outside the Project workspace.
  • Using File Class represent a file in Java, which is available inside the Project workspace.
    • Absolute Path
    • Shortcut Path
  • Representing a File and creating it
    • File f = new File(“fileName.extension”);
    • createNewFile()
    • exists() method
  • Representing a Folder and creating it
    • File f = new File(“FolderName”);
    • mkdir();
    • exists() method
  • Creating the file at the desired folder location
    • File f2 = new File(f1,”fileName.extension”);
  • File Methods
    • isFile()
    • isDirectory()
    • delete()
  • FileWriter
    • write(String s)
    • flush()
    • close()
  • FileReader
    • read()
    • close()
  • BufferedWriter
    • write(String s)
    • newLine()
    • flush()
    • close()
  • BufferedReader
    • readLine()
  • PrintWriter
    • println()
    • print()
    • flush()
    • close()
  • We can use FileOuputStream for writing into the file and FileInputStream for reading from file.
    • fos.write(textToBeWritten.getBytes());
    • fos.flush();
    • fis.read();
    • (char)fis.read();

Collections Framework

Collection is a group of individual Objects.

  • Array’s are fixed in sized, where as Collections are grow-able in size
  • Though Collections Framework is a vast subject, we have to only learn the below for Selenium:
  • ArrayList 
    • ArrayList is nothing but a re-sizable array and is not of fixed size
    • Demonstrate an ArrayList which stores integer values and uses for loop to print those values
    • Demonstrate an ArrayList which stores different types of values and uses for each loop to print those values
    • Assigning the object of ArrayList class to Collection / List Interface – Demonstrate
  • HashSet 
    • Unlike ArrayList, HashSet wont have index values and hence we cannot use for loop with HashSet
    • Unlike ArrayList, HashSet stores the values in a random order
    • Demonstrate HashSet which stores integer type of values and uses for each loop to print those values
    • Assigning the object of HashSet class to Collection / Set Interface
  • Iterator interface and iterator() method
    • iterator() is a predefined method of Collection interface, who’s return type is Iterator interface
    • hasNext() and next() are the predefined methods of the Iterator interface
    • Demonstrate using Iterator and iterator() with ArrayList
    • Demonstrate using Iterator and iterator() with HashSet
  • HashMap
    • Instead of storing the objects as a group of Objects, HashMap stores the objects in the form of key value pairs.
    • Demonstrate a HashMap which stores different key value pairs and uses get() method to retrieve a value based on the provided key
    • Demonstrate a HashMap which stores different key value pairs and uses for each loop to print those values

By,
Arun Motoori

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

For FREE Testing Tutorials & Videos

X
Open chat
Contact Us on Whatsapp