*JAVA* Create a class called \"TextFile\". It should contain the following three
ID: 3730471 • Letter: #
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 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.");
}
}
}
Explanation / Answer
/****** Below is the Complete code . I have tested all three methods with all the boundary Conditions.
Note: While giving the file path as the input you must have to provide the full path of the file along with //
for example . If your file is present in the E drive in folder test with name sample then full path of the file will be
E://test//sample.txt
Please follow above mentioned full path. All the three methods will work perfectly fine.
*****/
package lab;
import java.util.Scanner;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class TestFileOperations{
public static void main(String args[]){
System.out.println("Welcome to TestFileOperations");
// For reading a file
System.out.println("Reading a File:");
Scanner input = new Scanner(System.in);
String path = "";
System.out.println("Enter path to the file which you want to read :");
path = input.nextLine();
System.out.println("Path: " + path);
String readData = TextFile.read(path);
System.out.println("Writing to a File");
input = new Scanner(System.in);
path = "";
System.out.println(" Enter path to the file where you want to Write:");
path = input.nextLine();
System.out.println("Path: " + path);
System.out.println(" Enter Text to be written:");
String write = input.nextLine();
TextFile.write(write, path);
System.out.println("Appending to a File");
input = new Scanner(System.in);
System.out.println(" Enter path to the file where you want to append:");
path = input.nextLine();
System.out.println("Path: " + path);
System.out.println(" Enter Text to be appended:");
write = input.nextLine();
TextFile.append(write, path);
}
}
class TextFile {
public static String read(String filepath) {
String line = "";
String out = "";
try {
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine())!= null) {
out += (line + " ");
}
br.close();
System.out.println("Read Sucess Data in " + filepath + " is:");
System.out.println(out);
} catch (IOException e) {
System.out.println("Error: Cannot read the file.");
}
return out;
}
public static void write(String text, String filepath) {
/*
Scanner
*/
String lines[] = text.split(" ? ");
try {
FileWriter fw = new FileWriter(filepath);
BufferedWriter bw = new BufferedWriter(fw);
for(String line : lines){
bw.write(line);
bw.newLine();
}
bw.close();
System.out.println("Completed. Written To File " + filepath);
} catch (IOException 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();
*/
String lines[] = text.split(" ? ");
try {
FileWriter fw = new FileWriter(filepath, true);
BufferedWriter bw = new BufferedWriter(fw);
for(String line : lines){
bw.write(line);
bw.newLine();
}
bw.close();
System.out.println("Completed. Written To File " + filepath);
} catch (IOException e) {
System.out.println("Unable to Write to the file.");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.