1. Introducation to File Handling
2. Introducation to Command line argument
4. Write only first letter of string in the file.
5. Read only first letter of string in the file.
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.
Create one empty file :
First of all you can create one empty file (.txt) or create that empty file using command line arguments.
Example for Write only first letter of string in the file:
package gsh.java.programs;
import java.io.*;
public class WriteFileDemo2 {
public static void main(String[] args) {
try{
FileWriter writeContent = new FileWriter("D:\\fileHeading\\temp.txt");
for (String arg : args) {
writeContent.write(arg, 0, 1);
}
writeContent.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
Command line argument : Welcome To Getsolutionhub.
Output : WTG
Example for Read only first letter of string in the file :
package gsh.java.programs;
import java.io.*;
import java.util.Scanner;
public class ReadFileDemo2 {
public static void main(String[] args) {
try{
File readContent = new File("D:\\fileHeading\\firstLetter.txt");
Scanner sc = new Scanner(readContent);
while(sc.hasNextLine()) {
String strLines=sc.nextLine();
System.out.println(strLines);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
Output:
WTG
0 Comments
Enter Your Comment