Question

In: Computer Science

in java: Write a class responsible for storing data in a text file. The class should...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a JAVA program that reads a text file. Text file contains three lines. Place each...
Write a JAVA program that reads a text file. Text file contains three lines. Place each line in an array bucket location. Add to the end of the array list. Print the array.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
write a program in Java that can take a given text file and then compress it...
write a program in Java that can take a given text file and then compress it with Huffman coding due 20 october 2020 Huffman coding can be used to “zip” a text file to save space. You are required to write a program in Java that can take a given text file and then compress it with Huffman coding. Your program should be able to decompress the zipped files as well [4 marks]. In addition, show percentage gain in data...
in java please!!! suppose there is a text file named TextFile.txt with some data in the...
in java please!!! suppose there is a text file named TextFile.txt with some data in the root directory (e.g. C:\ or \) of your computer. In JAVA, write code that (i) opens the file in read mode; (ii) displays the file content on the monitor screen/console; (iii) uses exception handling mechanism with try, catch, and finally blocks; and (iv) safely closes the file. Writing the main() method is optional.
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
In Java language. Write a brief program that writes your name to a file in text...
In Java language. Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT