13 – Java IO

Selenium Class 13 – Java IO

Java Input and Output Operations

Input:
Input using input devices (keyboard & Mouse)
Read data from Files (text/excel/database files etc…)
Read data from Application objects/elements

Output:
Display message/s on the Console (Computer Console/Eclipse IDE Console)
Display Variable values….
Display Variable values and message/s
Write data to a file/s….

There are three ways available in Java for reading input,

1) Scanner
2) DataInputStream
3) BufferedReader

Note: using java.util.Scanner is the easier way and it includes many methods to check input is valid or not

Example for Reading input using input devices

Scanner scan = new Scanner (System.in); //Input Stream

System.out.println(“Enter Your Name”);
String name = scan.nextLine();
System.out.println(“Your name is: “+name);

System.out.println(“Enter a City”);
String city = scan.next();
System.out.println(“Your City is: “+city);

System.out.println(“Enter Your Mobile Number”);
long mobile = scan.nextLong();
System.out.println(“Your Mobile Number is: “+mobile);

System.out.println(“Enter your ID”);
int id = scan.nextInt();
System.out.println(“Your ID is: “+id);

System.out.println(“Enter a Value”);
boolean val=scan.nextBoolean();
System.out.println(“Your value is: “+val);

System.out.println(“Enter a Number with decimal places”);
double d = scan.nextDouble();
System.out.println(“Your Number is: “+d);

System.out.println(“Enter a Charcter”);
char a = scan.next().charAt(0);
System.out.println(“Your Char is: “+a);

Read a value multiple times using for loop

for (int i=1; i<=5; i++){
System.out.println(“Enter a Character”);
char a = scan.next().charAt(0);
System.out.println(“Your Char is: “+a);
}

Display Output on the Console:

int a=10, b=20;

System.out.println(a);//10
System.out.println(a+b);//30
System.out.println(“Welcome to Java World”);

System.out.println(“a Value is: “+a);//a value is: 10
System.out.println(“Addition of a, b is: “+(a+b));
System.out.println(“”);//Blank Line
System.out.println(“a value is: “+a +” b value is: “+b);

File Handling…

  • User point view Text File/Flat File, Excel File, Word Doc, PDF etc…
  • Computer point view, everything is file – Drive, Folder, File, Database File….

In VBScript – IO Operations & File Handling

Input and Output Operation –
Input using input devices – InputBox (built-in Function)
Display Output – MsgBox (built-in Function)

File Handling:
i) Handling Drivers/Folders/Text Files – using FileSystem Object
ii) Handling Excel Application – Using Excel Application Object
iii) Handling Word Application – Using Word Application Object
iv) Handling Databases – Database Connection, Database Recordset, Database Command Objects)

In Java:

Input and Output Operations – Using Scanner and System predefined classes

File Handling:
File Class – Drive, Folder, Flat File….
Reading & Writing for Flat files (BufferedReader and BufferedWriter)
Excle File – jxl.jar…
*Import third party jar files and add to Java Project and handle….

Examples:

1) Create a Folder on Desktop

File fileObject = new File (“C:/Users/admin/Desktop/Selenium”);
fileObject.mkdir();

2) Delete a Folder

File fileObject = new File (“C:/Users/admin/Desktop/Selenium”);
fileObject.delete();

3) Create a Text File

File fileObject = new File (“C:/Users/admin/Desktop/Selenium.txt”);
fileObject.createNewFile();

File fileObject = new File (“C:/Users/admin/Desktop/Selenium.xls”);
fileObject.delete();

Note: Using File Object we can create and delete all type files, but we can’t conduct internal operations