import java.io.*;
class demo1
{
public static void main(String args[]) throws IOException
{
char ch;
String str;
System.out.println("\nA.Filehandling\nB.Fileout\nC.Filein\n D.Fileread\nE.Filewrite\nF.Filerename and delete");
System.out.println("\n\nEnter your choice");
ch=(char)System.in.read();
switch(ch)
{
case 'a':
// To get some lines of text as input and writing it in another file. try
{
File f1=new File(args[0]);
System.out.println("file name:"+f1.getName());
System.out.println("path:"+f1.getPath());
System.out.println("Absolute Path:" +f1.getAbsolutePath());
System.out.println(f1.exists()?"file exists":"file does not exists");
System.out.println(f1.is Directory()?"fileisa directory":"file is not a directory");
System.out.println(f1.isFile()?"file is an ordinary file":"file may be a named pipe");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter the file name to be searched as a command line argument.");
}
break;
case 'b':// To read a file and displaying the contents on the console.
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
FileOutputStream fos=new FileOutputStream(args[0]);
System.out.println("Enter lines of text. Type stop to finish the process.");
while(true)
{
str=br.readLine();
if(str.equals("stop"))
break;
str=str+"\r\n";
byte buf[]=str.getBytes();
fos.write(buf);
}
fos.close();
System.out.println("Contents written to "+args[0]);
}
catch(IOException e)
{
System.out.println("Exception Caught: "+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter the name of the output file as a command line argument.");
}
break;
case 'c':
// To read a file and displaying the contents on the console, using //FileReader.
try
{
FileInputStream fis=new FileInputStream(args[0]);
int size=fis.available();
for (int i=0;i
System.out.print((char)fis.read());
fis.close();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter the file name to be read as a command line argument.");
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
}
break;
case 'd'://To copy the contents of a file to another file, using FileWriter.
try
{
FileReader fr=new FileReader(args[0]);
BufferedReader br=new BufferedReader(fr);
while((str=br.readLine())!=null)
System.out.println(str);
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("Exception Caught: "+e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter the name of the input file as a command line argument.");
}
break;
case 'e':// To rename and deleting a file.
try
{
FileReader fr=new FileReader(args[0]);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(args[1]);
while((str=br.readLine())!=null)
{
str=str+"\r\n";
char buf[]=new char[str.length()];
str.getChars(0,str.length(),buf,0);
fw.write(buf);
}
fr.close();
fw.close();
System.out.println("File Writer process finished successfully.");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Enter the names of the input file and output file as a command line argument.");
}
break;
case 'f':
if(args.length==2)
{
File f=new File(args[0]);
File f1=new File(args[1]);
if(f.exists())
{
System.out.println(f+" does exists."); System.out.println("Its size is "+f.length()+" bytes");
f.renameTo(f1);
System.out.println("Renamed file name: "+f1 );
System.out.println("deleting the renamed file "+f);
System.out.println("**********************");
f.delete();
}
else
System.out.println(f+" does not exists");
}
else
System.out.println("Enter the file name to be renamed and the new name of the file as a command line argument.");
break;
default:
System.out.println("Enter the correct choice");
break;
}
}
}
OUTPUT:
Z:\jdk1.5\bin>javac demo1.java
Z:\jdk1.5\bin>java demo1 "input.txt"
A.Filehandling
B.Fileout
C.Filein
D.Fileread
E.Filewrite
F.Filerename and delete
Enter your choice a
file name:input.txt
path:input.txt
Absolute path:Z:\jdk1.5\bin\input.txt
file exists
file is not a directory
file is an ordinary file
Z:\jdk1.5\bin>java demo1 "input.txt"
A.Filehandling
B.Fileout
C.Filein
D.Fileread
E.Filewrite
F.Filerename and delete
Enter your choice b
Enter lines of text. Type stop to finish the process.
java programming
stop
Contents written to input.txt
Z:\jdk1.5\bin>java demo1 "input.txt"
A.Filehandling
B.Fileout
C.Filein
D.Fileread
E.Filewrite
F.Filerename and delete
Enter your choice c
java programming
Z:\jdk1.5\bin>java demo1 "input.txt" "out.txt"
A.Filehandling
B.Fileout
C.Filein
D.Fileread
E.Filewrite
F.Filerename and delete
Enter your choice e
File Writer process finished successfully.
Z:\jdk1.5\bin>java demo1 "input.txt" "in.txt"
A.Filehandling
B.Fileout
C.Filein
D.Fileread
E.Filewrite
F.Filerename and delete
Enter your choice f
input.txt does exists.
Its size is 20 bytes
Renamed file name: in.txt
deleting the renamed file input.txt
No comments:
Post a Comment