1. Introducation to File Handling
2. Introducation to Command line argument
3. Example for create a file using command line argument
4. Example for write content into the file using command line argument
5. Example for read file content written by using command line argument
6. Example for delete any file using command line argument
Introducation to File Handling :
File handling in Java is defined as reading and writing data to a file. The particular file class from the package called java.io allows us to handle and work with different formats of files. Thus, if we want to use a file class, we need to create an object of that particular class and should specify the filename or directory name.
Introducation to Command line argument :
Java command-line argument is an argument i.e. passed at the time of running the Java program. In command-line, the arguments passed from the console can be received in the java program and it can be used as an input. The users can pass the arguments during the execution bypassing the command-line arguments inside the main() method.
Example for create a file using command line argument :
package gsh.java.programs;
import java.io.*;
public class CreateFileDemo {
public static void main(String[] args) {
try{
for (int i=0;i<args.length;i++){
File createFile = new File(args[i]);
createFile.createNewFile();
}
System.out.println("File has been created...");
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
File has been created...
Command line Argument : D:\\fileHeading\\java.txt
//Go to your define path and check file has been created successfully..
Example for Write content into created file using command line argument :
package gsh.java.programs;
import java.io.*;
public class WriteFileDemo {
public static void main(String[] args) {
try{
FileWriter writeContent = new FileWriter("D:\\fileHeading\\java.txt");
for (String arg : args) {
writeContent.write(arg+" ");
}
writeContent.close();
System.out.println("File is written ! ");
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
Command line Argument : Welcome to java industry...
written in the file !
//Go to your define path and check file has been written successfully..
Example for read file content written by using command line argument :
package gsh.java.programs;
import java.io.*;
import java.util.*;
public class ReadFileDemo {
public static void main(String[] args) {
try{
File readContent = new File("D:\\fileHeading\\java.txt");
Scanner sc = new Scanner(readContent);
while(sc.hasNextLine()) {
String strLines=sc.nextLine();
System.out.println(strLines);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
Welcome to java industry...
Example for delete any file using command line argument :
package gsh.java.programs;
import java.io.*;
public class DeleteFileDemo {
public static void main(String[] args) {
try{
for (int i=0;i<args.length;i++){
File deleteFile = new File(args[i]);
if(deleteFile.delete()){
System.out.println("File has been deleted...");
}else{
System.out.println("Some error occurred...");
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
Command line Argument : D:\\fileHeading\\java.txt //define here file location that you want to delete...
File has been deleted...
//Go to your define path and check file has been deleted successfully..
0 Comments
Enter Your Comment