In: Computer Science
in java:
Write a class responsible for storing data in a text file. The class should include a method that accepts a string and appends it to a file.
CODE:
import java.io.FileWriter;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name of file or Path: ");
//input of name of file
String filename = sc.nextLine();
//Try block to handle the JavaIO Exception
try{
//Opening given file in append mode
FileWriter fw=new FileWriter(filename,true);
System.out.println("Enter Text to write in "+filename);
//input of text from user to be appended in file
String text = sc.nextLine();
//appending in the file
fw.write(text);
//Closing the file
fw.close();
}
//printing Exception if any occurs
catch(Exception e){
System.out.println(e);
}
//Acknowledgement for success..
System.out.println("Success...");
}
}
OUTPUT:
FILE SCREENSHOT: