27-09-2016, 01:04 PM
1456336237-JavaPractical.docx (Size: 49.19 KB / Downloads: 5)
A. Program to demonstrate working of Command-Line Arguments :-
public class Argument
{
public static void main(String[ ] args)
{
System.out.print("Hi, ");
System.out.print(args[0] + " " );
System.out.print(args[1] + " " );
System.out.println(". How are you?");
}
}
OUTPUT :
Hi, Java Programs . How are you?
B. Parsing Numeric Command-Line Arguments :-
public class Argument
{
public static void main(String[ ] args)
{
int number1 = Integer.parseInt(args[0]);
int number2 = Integer.parseInt(args[1]);
int sum = number1 + number2;
System.out.print("The sum of two number is : " + sum);
}
}
OUTPUT :
The sum of two number is : 30
Question -2
WAP to demonstrate the creation and initialization of Input Streams.
// BufferedCopy.java
import java.io.*;
classBufferedCopy
{
public static void main (String [] args)
{
if (args.length != 2)
{
System.out.println ("usage: java BufferedCopysrcpathdstpath");
return;
}
BufferedInputStreambis = null;
BufferedOutputStreambos = null;
try
{
FileInputStreamfis = new FileInputStream (args [0]);
bis = new BufferedInputStream (fis);
FileOutputStreamfos = new FileOutputStream (args [1]);
bos = new BufferedOutputStream (fos);
int byte_;
while ((byte_ = bis.read ()) != -1)
bos.write (byte_);
System.out.println ("Data Copied");
}
catch (FileNotFoundException e)
{
System.out.println ("File not found");
// Do other stuff related to that exception (if necessary).
}
catch (IOException e)
{
System.out.println ("I/O Problem: " + e.getMessage ());
// Do other stuff related to that exception (if necessary).
}
finally
{
if (bis != null)
try
{
bis.close ();
}
catch (IOException e)
{
}
if (bos != null)
try
{
bos.close ();
}
catch (IOException e)
{
}
}
}
}
Command :-C:\>java BufferedCopy Copy.java Copy.txt
Output :-Data Copied