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.
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...
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.
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT