Sunday, February 15, 2009

How to pass file as input in java program and parsing it.

There are different ways we can pass the file path as a parameter to a java file and can be parsed. One of the very good class provided by Sun is java.util.Scanner class. This class provides sufficient methods to accpet file as an input parameter and also provides methods to parse it.Here I am demonstrating you two different ways of passing file to a java program as well as parsing it.
# FileScanner.java
------------------------------
/* File : FileScanner.java
Purpose: Accepts file name as input and prints the content after parsing.
Author : Anaparthi Subrahmanyam
Published: 2009-02-15
Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage
*/
import java.util.Scanner;

import java.io.File;
public class FileScanner{
public static void main(String[] args) {
// creating an scanner object to recieve the Input
System.out.println("Please enter the complete file name including absolute path");
Scanner in = new Scanner(System.in);
// Following two variable will be used later
Scanner sn =null;
File f = null;
// accepting user input
String fname = in.next();
System.out.println("File Name Entered : " + fname);
f = new File(fname);
try
{
if(!f.exists()) throw new Exception(" File does not exists!");
// Creating scanner object using constructior Scanner(File f)
// This obj will be parsed using methods provided by the Scanner Class
sn = new Scanner(f);
// Parsing and printing untill end of line of the file provided.
while(sn.hasNext())
{
System.out.println(sn.next());
}
} catch (Exception e) { e.printStackTrace(); }
}
}
--------------------------
Compile it and just execute the class file, once the file gets started executing, it will ask for the file name . Once you entered the file name, press enter. Complete file content will be displayed on console.

The other way of using the Scanner class is as follows:
/* File : FileScanner2.java
Purpose: Accepts file name as input and prints the content after parsing.
Author : Anaparthi Subrahmanyam
Published: 2009-02-15
Usage Notification: Author doesnt own responsibility of any loss of data, responsibility for misuse or loss of any nature if this code is used. If possible, please provide link to this webpage
*/import java.util.Scanner;
import java.io.File;
public class FileScanner2{
public static void main(String[] args) {
String fname = args[0];

// Following two variable will be used later
Scanner sn =null;
File f = null;
// accepting user input
String fname = in.next();
System.out.println("File Name Entered : " + fname);
f = new File(fname);
try
{
if(!f.exists()) throw new Exception(" File does not exists!");
// Creating scanner object using constructior Scanner(File f)
// This obj will be parsed using methods provided by the Scanner Class
sn = new Scanner(f);
// Parsing and printing untill end of line of the file provided.
while(sn.hasNext())
{
System.out.println(sn.next());
}
} catch (Exception e) { e.printStackTrace(); }
}
}
--------------------------

In the above program, pass the file name while executing the java class file such as :
cmd_prompt> java FileScanner2 c:\test\sample.txt
It will print the content of the file on the console.

No comments:

Post a Comment