io and file handling in Java

I/O and File Handling in Java.
 

There are three ways available for reading input:
i) Scanner

ii) DataInputStream

iii) BufferedReader
---------------
Using java.util.Scanner is the easier and includes many methods to check input data is valid to read.

Example:---------------
package javaiooperations;
import java.util.Scanner;
public class ReadInput {
public static void main(String [] args){
    Scanner scan = new Scanner(System.in);
   
    System.out.println("Enter Your Name");
    String s1 = scan.nextLine();
    System.out.println("Your Name is: " + s1);
   
    System.out.println("Enter Your City Name");
    String s2 = scan.next();
    System.out.println("Your City Name is: " + s2);
   
    System.out.println("Enter Your Number");
    int a = scan.nextInt();
    System.out.println("Your Number is: " + a);
   
    System.out.println("Enter Your City Name");
    double b = scan.nextDouble();
    System.out.println("Value is: " + b);
    scan.close();
}
}
-------------------------------------------
File handling in Java----------------------
Two categories of File System Operations

i) High level operations / External operations

Create a folder

delete a folder

create a text file

delete a text file etc...

ii) Low level operations / Internal operations

Read data

Write data etc...
------------------------
Using File Class we can perform High level operations
Examples:
1) Create a Folder
package javaiooperations;

import java.io.File;

public class CreateFolder {
   
    public static void main(String [] args){
        File fileObject = new File("C:/Users/G C Reddy/Desktop/Selenium");
        fileObject.mkdir();
        }
}
---------------------------------
2) Check the existence of Selenium Folderpublic static void main(String [] args){
        File fileObject = new File("C:/Users/G C Reddy/Desktop/UFT");
        boolean a = fileObject.exists();
        System.out.println(a);
        }
-------------------------------------
3) Delete a Folder
public static void main(String [] args){
        File abc = new File ("C:/Users/G C Reddy/Desktop/Selenium");
        abc.delete();
        boolean a = abc.exists();
        //System.out.println(a);
        if (a == true) {
            System.out.println("Folder exists");
        }
        else{
            System.out.println("Folder doesn't exist");   
        }
        }
-----------------------------------
4) Create a Text file
public class CreateFolder {
   
    public static void main(String [] args){
        File abc = new File ("C:/Users/G C Reddy/Desktop/Selenium.txt");
        try {
            abc.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
-------------------------------------------
5) Delete a text file    public static void main(String [] args){
        File abc = new File ("C:/Users/G C Reddy/Desktop/Selenium.txt");
        abc.delete();
       
    }
------------------------------------      

1 comments:

sir, your notes is so simple and easy to understand.really good.please add notes for file low level operations.
Thanks

Post a Comment