Question: *JAVA* Create a class called TextFile. It should contain the following three static methods: public static String read(String filepath) returns the contents of the indicated
*JAVA*
Create a class called "TextFile". It should contain the following three static methods:
public static String read(String filepath)
returns the contents of the indicated text file -- remember to include line breaks.
public static void write(String text, String filepath)
writes the text to the indicated text file.
public static void append(String text, String filepath)
appends the text to the indicated text file.
Then create a separate class called "TestFileOperations". It should contain a main method that tests the static methods of the TextFile class.
Note that if I can find a way to crash this program -- i.e. it produces an unhandled exception -- than that will cost you points.
This is what I have so far (This is not the main):
package lab;
import java.util.Scanner; import java.io.FileReader; import java.io.BufferedReader; import java.io.FileWriter; import java.io.BufferedWriter;
public class TextFile {
public static String read(String filepath) { Scanner input = new Scanner(System.in); String path = ""; String line = "";
System.out.println("Enter path to the file:"); path = input.nextLine(); System.out.println("Path: " + path);
try { FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr); line = br.readLine(); String out = "";
while (line != null) { out += (line + " "); line = br.readLine();
} br.close(); System.out.println("Read Sucess Data in " + path + " is:"); System.out.println(out); } catch (Exception e) { System.out.println("Error: Cannot read the file."); } return line; }
public static void write(String text, String filepath) { Scanner input = new Scanner(System.in); String path = ""; String line = "";
System.out.println(" Enter path to the file:"); path = input.nextLine(); System.out.println("Path: " + path); System.out.println(" Enter Text to be written:"); String write = input.nextLine();
try { FileWriter fw = new FileWriter(path); BufferedWriter bw = new BufferedWriter(fw); bw.write(write + " "); bw.close(); System.out.println("Completed. Written To File " + path); } catch (Exception e) { System.out.println("Unable to Write to the file."); } }
public static void append(String text, String filepath) { Scanner input = new Scanner(System.in); String path = ""; String line = ""; boolean append = false;
System.out.println(" Enter path to the file:"); path = input.nextLine(); System.out.println("Path: " + path); System.out.println(" Enter Text to be written:"); String write = input.nextLine();
try { FileWriter fw = new FileWriter(path, append); BufferedWriter bw = new BufferedWriter(fw); bw.write(write + " "); bw.close(); System.out.println("Completed. Written To File " + path); } catch (Exception e) { System.out.println("Unable to Write to the file."); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
