Program to print the source code in the output


Hey there guys, this is a simple Java program which will print the source code of the program.

Concept: 
First of all to achieve this output we must know a bit of the program concept. To perform this operation we must use FileInputStream concept. 
                  
The basic requirement is to include the import statement FileInputStream in the program, which will import the required classes and interfaces which will be useful to perform this action. For that we have to write "import java.io.*".

The total flow of the program lies in the fews steps written below.
  1.  Create FileInputStream class object
  2.  Get data size from FileInputStream
  3.  Prepare byte[] (byte array) with the identified data size
  4.  Read data from FileInputStream into byte[]
  5.  Convert byte[] data into string
  6.  Close FileInputStream

Program code:

import java.io.*;
public class FileInput
{
  public static void main(String[] args) throws Exception
  {
    FileInputStream fis= new FileInputStream("FileInput.java");
    int size=fis.available();
    byte[] b=new byte[size];
    fis.read(b);
    String data=new String(b);
    System.out.println(data);
    fis.close();
  }
} 
Demo Output:
After running the program we will get this type of output. This is the Linux Format command prompt, windows command prompt format may differ from this image, but output will be the same.

Description:
  •  The first statement is the import statement which can be clearly known.
  •  Then the FileInput class is created, under which in the main() method the FileInputStream class object is created in the 6th line.
  •  Then size is calculated in the 7th line by calling the available() method and byte[] is specified in the 8th line.
  •  Then by read() method the input is taken and then in the next line the byte[] data is converted to string.
  •  Then the output is shown by System.out.println() and then the FileInputStream is closed by close() method.


                       Well thats it. A simple program to print the source code in the output. You can give feedbacks in the comments below or you can contact me to my mail ID given in the profile.

Thanks for reading!
                  

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution