JAVA

Java File Handling

Java File Handling Example - Java File Handling

Java File Handling:

Java file handling is the variable which used to store the values as temporary that release the value after processing instruction. Java provides a process to store values in the form of a file and can access the value after terminating the program.

 

java file
Image Source:- http://www.studytonight.com

 

File Input Stream

An input stream is a class that available in the java.io package. It provides built-in constructor or functions to read the data from a file for processing.

 

java classes
Image Source:- https://i2.wp.com

 

  1. read():- It is a function that read the data from the file into bit format.
  2. close():- To stop the processing of existing file.

 

input stream
Image Source:- http://hepunx.rl.ac.uk

 

Example:-
import java.io.*;
Class A
{
   public static void main (String s[])
    {
       FileInputStream f = new FileInputStream ("anna.java");
       byte b[] = new byte [2000];
        f.read (b);
        String p = new String (b, 0, b.length);
        System.out.print (p.trim());
         }
}

 

trim
It removes blank spaces.
String p = new String (b, 0, b.length)
It is an instruction that convert the byte data into stream format. In this, we pass byte array starting index position and last index position as an argument.

 

File Output Stream

Output Stream is a class that allows managing output related instruction. It means we can write the data into a file.

  1. write():- It is a function that writes the byte data into existing file.
  2. close():- To stop the processing of existing file.

 

output stream
Image Source:- http://hepunx.rl.ac.uk

 

Example:-
import java.io.*;
Class A
{
   public static void main (String s[])
    {
        FileOutputStream f = new FileOutputStream ("anna.java");
         byte b[] = new byte [2000];
         System.in.read (b);
          f.write (b);
          f.close();
            }
}

 

System.in.read:- It is a function that gets the input through user in byte format and returns length.

 

Program to copy from one file to another file
import java.io.*;
Class A
{
   public static void main (String s[])
   {
      FileOutputStream f = new FileOutputStream ("anna.java");
      byte b[] = new byte [2000];
      FileInputStream f1 = new FileInputStream ("x.java");
      f1.read (b);
      f.write (b);
       f1.close ();
       f.close ();
        }
}

 

Command Line Argument

Command Line Argument is a process for getting the input through user at the command line. It means the user can pass the data at the time of program execution. We define values at the command line that automatically store in string variables which are defined by the main function. The values automatically store at different index position, the first argument value store at zero and another at next position and so on.

 

program
Image Source:- http://www.mathcs.emory.edu

 

Example:-
Class Aao
{
   public static void main (String s[])
   {
      int a = Integer.parseInt(s[0]);
      int b = Integer.parseInt(s[1]);
      int c = a+b;
      System.out.print ("sum is" + c);
        }
}

 

Integer.parseInt():- It is a function that converts string data into integer format. In this, we pass a string as an argument.

 

BufferedReader

Buffer Reader is a class that available in a java.io package that also allows getting the input from the user.

buffer reader
Image Source:- http://www.studytonight.com

 

Read():- 

It is a function that gets character input from the user.

Example:-
import java.io.*;
Class Aao
{
   public static void main (String s[])
    {
       BufferedReader b = new BufferedReader (new InputStreamReader (System.in));
       System.out.print ("enter char");
       char c = (char) b.read();
       System.out.print ("ASCII value is" + (int)c);
        }
}

 

readline():-

It is a function that allows getting String input through a user.

Example:-
import java.io.*;
Class Aao
{
   public static void main (String s[])
   {
      BufferedReader b = new BufferedReader (new InputStreamReader (System.in));
      System.out.print ("enter string");
      String s = b.readline();
      System.out.print ("ASCII value is" + s);
      }
}

 

Wrapper Class

Java programming language provides built-in classes to control the data type conversion at execution time. The class identifier and data type name are same. The class name similar as the data type is known as a wrapper class.

 

Types of Wrapper Classes

 

tabel
Image Source:- https://image.slidesharecdn.com

 

A) Integer:- It is a class that allows managing different data type into integer format at execution time.

  • parseInt():- It is a function that converts the string data into integer format.
  • Float Value:- It is a class that allows managing the float data.
  • double Value:- It is used to manage double data type data.

B) Float Class:- It is a wrapper class for managing the float data type data from other variables.

  • parseFloat():- It is a function that converts float from a string.
  • int Value:- To manage the integer data.
  • double Value:- To manage the double data type data.
  • toShort:- To manage the short data type data.
  • toString:- It is a function that manages the given data into String format.

 

Character Class

Character Class is a class that allows managing the character related data type.

 

table
Image Source:- http://www.c-jump.com

 

  1. isSpace():- It returns logical value after checking the given character isSpace or not.
  2. isDigit():- This function returns logical value like true or false after checking the existing value isDigit or not.
  3. isUpper():- It returns logical value that checks the given character is in the upper class.
  4. isLower():- It returns logical value that checks the given character is in the lower class.
  5. toUpperCase():- It is used to convert lower case character into upper case.
  6. toLowerCase():- It is used to convert upper case character into lower case.

 

Example:-
import java.util.*;
Class B
{
   public static void main(String s[])
    {
       Char c[] = new Char[20];
       int p=0, t=0, n=0, z=0, i;
       Scanner sc = new Scanner(System.in);
       System.out.print("enter value");
       String s = sc.nextLine();
       int l=s.length();
       c=s.toCharArray();
       for (i=0;i<l;i++)
       {
          if(Character.isDigit(c[i]))
          {
              p++;
               }
         if(Character.isSpace(c[i]))
         {
            t++;
             }
         if(Character.isUpperCase(c[i]))
        {
            n++;
             }
        z++;
        }
     System.out.println("no of chars"+z);
     System.out.println("no of space"+t);
     System.out.println("no of digit"+p);
     System.out.println("no of upper"+n);
}
}

 

About the author

Tutorial Circle

Add Comment

Click here to post a comment