Input Output Operations in addition to File Handing inwards Java
i) Input in addition to Output Operations
ii) File Handling inwards Java
iii) Exception Handling inwards Java
--------------------------------
i) Input in addition to Output Operations
There iii ways available for reading input.
a) Scanner
b) DataInputStream
c) BufferedReader
------------------------------
Using java.util.Scanner is the easier agency in addition to it includes many methods to cheque input is valid to read.
Read Input Example:
public static void principal (String [] args){
Scanner scan = novel Scanner(System.in); //System.in is an input stream
System.out.println("Enter Your Name");
String advert = scan.nextLine();
System.out.println("You are Name is "+name);
System.out.println("Enter Your City");
String urban gist = scan.next();
System.out.println("Your City is "+ city);
System.out.println("Enter a Number");
int num = scan.nextInt();
System.out.println("Your Number is "+num);
System.out.println("Enter a Mobile Number");
long num2 = scan.nextLong();
System.out.println("Your Mobile Number is "+num2);
System.out.println("Enter a Value");
double num3 = scan.nextDouble();
System.out.println("Your Value is "+num3);
System.out.println("Enter a Character");
char a = scan.next().charAt(0);
System.out.println("Your Char is "+a);
System.out.println("Enter a Value");
boolean val = scan.nextBoolean();
System.out.println("Your Value is "+val);
scan.close();
--------------------------------------------------
Display Output on the Console
int a=10, b=20;
System.out.println("Welcome to Selenium");//Welcome to Selenium
System.out.println("Value b is "+b); //Value b is 20
System.out.println("Value a is "+a + " Value b is "+b); //Value a is 10 Value b is 20
--------------------------------
ii) File Handling inwards Java
Using File Class nosotros tin handgrip Computer files.
Examples
1) Create a Folder
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.mkdir();
}
2) Check the existence of Selenium Folder.
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
boolean a = fileObject.exists();
if (a == true){
System.out.println("Folder Exists");
}
else {
System.out.println("Folder Not Exists");
}
}
-------------------------------------------------
3) Delete a Folder
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.delete();
}
-------------------------------------
4) Create a Text File
public static void principal (String [] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT2.xls");
fileObject.createNewFile();
}
5) Delete a Text File
public static void principal (String [] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.txt");
fileObject.delete();
}
--------------------------------
iii) Exception Handling inwards Java
> An exception is an event, it occurs during execution of a program, when normal execution of the programme is interrupted.
> Exception treatment is machinery to handgrip exceptions.
Common scenarios where exceptions may occurs
1) Scenario where Arithmeticexception occursIf nosotros separate whatever publish past times Zero in addition to then Arithmeticexception occurs
Ex:
int a =10/0;
--------------------------
2) Scenario where NullPointerexception occurs.
if nosotros convey no value inwards whatever variable, performing whatever performance past times the variable.
Ex:
String second =null;
System.out.pritln(s.length()); //NullPointerexception
--------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatever value.
Ex:
String second = "abc";
int a = Integer.parseInt(s);
System.out.println(a);//NumberFormatException
--------------------------
// Convert information from String type to Integer
public static void principal (String [] args) {
Scanner scan = novel Scanner(System.in);
System.out.println("Read 2 Numbers");
String s1= scan.nextLine();
String s2= scan.nextLine();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
System.out.println(a+b);
scan.close();
---------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs.
if nosotros insert whatever value inwards the worng index.
Ex:
int [] a = novel int [5];
a[100] = 123;
System.out.println(a[100]); ArrayIndexOutOfBounds
----------------------------
Example:
public static void principal (String [] args) {
int a =10;
int b = 0;
int effect = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
----------------------------
Use endeavor grab block
Syntax:
try{
Statements
-----
------
}
catch (Exception name){
Exception treatment code
}
-------------------------
Example:
public static void principal (String [] args) {
int a =10;
int b = 0;
try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
-----------------------------
// Handling multiple exceptions
public static void principal (String [] args) {
int a =10;
int b = 0;
int abc [] = novel int[4];
try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e1){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
try
{
abc[30]=200;
System.out.println(abc[30]);
}
catch (ArrayIndexOutOfBoundsException e2){
System.out.println("Array Index Out of Bounds Error");
}
System.out.println("Hello Selenium");
}
--------------------------------
i) Input in addition to Output Operations
ii) File Handling inwards Java
iii) Exception Handling inwards Java
--------------------------------
i) Input in addition to Output Operations
There iii ways available for reading input.
a) Scanner
b) DataInputStream
c) BufferedReader
------------------------------
Using java.util.Scanner is the easier agency in addition to it includes many methods to cheque input is valid to read.
Read Input Example:
public static void principal (String [] args){
Scanner scan = novel Scanner(System.in); //System.in is an input stream
System.out.println("Enter Your Name");
String advert = scan.nextLine();
System.out.println("You are Name is "+name);
System.out.println("Enter Your City");
String urban gist = scan.next();
System.out.println("Your City is "+ city);
System.out.println("Enter a Number");
int num = scan.nextInt();
System.out.println("Your Number is "+num);
System.out.println("Enter a Mobile Number");
long num2 = scan.nextLong();
System.out.println("Your Mobile Number is "+num2);
System.out.println("Enter a Value");
double num3 = scan.nextDouble();
System.out.println("Your Value is "+num3);
System.out.println("Enter a Character");
char a = scan.next().charAt(0);
System.out.println("Your Char is "+a);
System.out.println("Enter a Value");
boolean val = scan.nextBoolean();
System.out.println("Your Value is "+val);
scan.close();
--------------------------------------------------
Display Output on the Console
int a=10, b=20;
System.out.println("Welcome to Selenium");//Welcome to Selenium
System.out.println("Value b is "+b); //Value b is 20
System.out.println("Value a is "+a + " Value b is "+b); //Value a is 10 Value b is 20
--------------------------------
ii) File Handling inwards Java
Using File Class nosotros tin handgrip Computer files.
Examples
1) Create a Folder
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.mkdir();
}
2) Check the existence of Selenium Folder.
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
boolean a = fileObject.exists();
if (a == true){
System.out.println("Folder Exists");
}
else {
System.out.println("Folder Not Exists");
}
}
-------------------------------------------------
3) Delete a Folder
public static void principal (String [] args){
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium");
fileObject.delete();
}
-------------------------------------
4) Create a Text File
public static void principal (String [] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT2.xls");
fileObject.createNewFile();
}
5) Delete a Text File
public static void principal (String [] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.txt");
fileObject.delete();
}
--------------------------------
iii) Exception Handling inwards Java
> An exception is an event, it occurs during execution of a program, when normal execution of the programme is interrupted.
> Exception treatment is machinery to handgrip exceptions.
Common scenarios where exceptions may occurs
1) Scenario where Arithmeticexception occursIf nosotros separate whatever publish past times Zero in addition to then Arithmeticexception occurs
Ex:
int a =10/0;
--------------------------
2) Scenario where NullPointerexception occurs.
if nosotros convey no value inwards whatever variable, performing whatever performance past times the variable.
Ex:
String second =null;
System.out.pritln(s.length()); //NullPointerexception
--------------------------
3) Scenario where NumberFormatException occurs
The incorrect formatting of whatever value.
Ex:
String second = "abc";
int a = Integer.parseInt(s);
System.out.println(a);//NumberFormatException
--------------------------
// Convert information from String type to Integer
public static void principal (String [] args) {
Scanner scan = novel Scanner(System.in);
System.out.println("Read 2 Numbers");
String s1= scan.nextLine();
String s2= scan.nextLine();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
System.out.println(a+b);
scan.close();
---------------------------
4) Scenario where ArrayIndexOutOfBounds exception occurs.
if nosotros insert whatever value inwards the worng index.
Ex:
int [] a = novel int [5];
a[100] = 123;
System.out.println(a[100]); ArrayIndexOutOfBounds
----------------------------
Example:
public static void principal (String [] args) {
int a =10;
int b = 0;
int effect = a/b;
System.out.println(result);
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
----------------------------
Use endeavor grab block
Syntax:
try{
Statements
-----
------
}
catch (Exception name){
Exception treatment code
}
-------------------------
Example:
public static void principal (String [] args) {
int a =10;
int b = 0;
try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
System.out.println("Hello Selenium");
}
-----------------------------
// Handling multiple exceptions
public static void principal (String [] args) {
int a =10;
int b = 0;
int abc [] = novel int[4];
try
{
int effect = a/b;
System.out.println(result);
}
catch (ArithmeticException e1){
System.out.println("Divided past times Zero Error");
}
System.out.println("Hello Java");
try
{
abc[30]=200;
System.out.println(abc[30]);
}
catch (ArrayIndexOutOfBoundsException e2){
System.out.println("Array Index Out of Bounds Error");
}
System.out.println("Hello Selenium");
}
--------------------------------