Programming Problem 3 – CycleFileInput Revisit the Cycle class in Unit 3. Modify
ID: 3598095 • Letter: P
Question
Programming Problem 3 – CycleFileInput Revisit the Cycle class in Unit 3. Modify your application such that the properties will be read from a text file called “CycleIn.txt”. Directions Examine your application for the class called Cycle. • Add an appropriate throws statement in the main method. • Create a reference to a File class with the appropriate name of a text file (Cycle.txt). Note: Cycle.txt was created in the previous assignment, CycleFileOutput. • In your code, check that the text file does exist. • Input the values from the file to memory. • Close the file. Verify the contents were written to the text file using notepad (or any word processor)
Explanation / Answer
The requirement not crystal clear. But I have tried my best to give the best solution. Please comment if anything wrong or you want modifications in the code.
code:-
-------------
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Cycle {
static File f = new File("");
static String s = f.getAbsolutePath();
static File cycleFileInput = new File( s+"/Cycle.txt"); //Taking file path
public static void main(String[] args) throws IOException {
if(cycleFileInput.exists()) { //Checking for file existence
System.out.println("Yes.. File exists");
}
Scanner sc = new Scanner(cycleFileInput); //Reading file
while(sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
//Writing text to file
FileWriter fw = new FileWriter(cycleFileInput, true);
PrintWriter pw = new PrintWriter(new FileWriter(cycleFileInput, true));
pw.append(" This is to modify...");
//Closing the streams
sc.close();
pw.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.