Question
I would like to see a sample of this code being used in a program. import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader;
I would like to see a sample of this code being used in a program.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner;
public class favorites { static File FILENAME = new File("listofFavorites.txt");
public static void main(String[] args) throws IOException { while(true) { System.out.println("Please provide number to perform operation"); System.out.println("1:Print list of favorites"); System.out.println("2:Add into list of favorites"); System.out.println("3:Delete from the list"); System.out.println("4:Exit"); Scanner sc=new Scanner(System.in); int input=sc.nextInt(); if(input==1) { readlist(); } else if(input==2) { System.out.println("Enter Item"); Scanner sc1=new Scanner(System.in); String item=sc1.next(); addintolist(" "+item); } else if(input==3) { // File inputFile = new File("myFile.txt"); File tempFile = new File("myTempFile.txt");
BufferedReader reader = new BufferedReader(new FileReader(FILENAME)); BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); String lineToRemove = ""; try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine; // while ((sCurrentLine = br.readLine()) != null) { // System.out.println(sCurrentLine); String currentLine; while((currentLine = reader.readLine()) != null) { System.out.println(currentLine); String trimmedLine = currentLine.trim(); if(trimmedLine.equals(lineToRemove)) continue; writer.write(currentLine + System.getProperty("line.separator")); System.out.println("want to remove (y/n)"); Scanner sc2=new Scanner(System.in); String flag=sc2.next(); if(flag.equalsIgnoreCase("y")) lineToRemove = currentLine; }
} catch (IOException e) { e.printStackTrace(); } writer.close(); reader.close(); boolean successful = tempFile.renameTo(FILENAME);
} else if(input==4) { System.out.println("exiting from program"); return; } else { System.out.println("invalid entry"); } } } public static void readlist() { try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) { System.out.println(sCurrentLine); }
} catch (IOException e) { e.printStackTrace(); } } public static void addintolist(String item) { try(FileWriter fw = new FileWriter(FILENAME, true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw)) { out.println(item); //more code //out.println("more text"); //more code } catch (IOException e) { //exception handling left as an exercise for the reader } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started