Java program to insert modify and delete lines into a masterfile.txt. For some r
ID: 648645 • Letter: J
Question
Java program to insert modify and delete lines into a masterfile.txt. For some reason i cannot get it to create the temp file and rename it for option 2 & 3. Any help is appreciated.
import java.io.*;
import java.nio.file.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Scanner;
public class master2{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1.insertion into file 2.modify into file 3.delete in file");
int choice=sc.nextInt();
try
{
File newTextFile = new File("masterfile.txt");
File temp = File.createTempFile("duplicateMaster", ".txt", newTextFile.getParentFile());
String charset = "UTF-8";
if(choice==1)
{
System.out.println("Enter String to insert");
String str = sc.next();
FileWriter fw = new FileWriter("masterfile.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(fw);
Scanner fileSC = new Scanner(newTextFile);
int count = 0;
while(fileSC.hasNextLine())
{
String line = fileSC.nextLine();
System.out.print("Line " + count + " " );
count++;
if (line.compareTo(str)==0)
{
System.out.println("Data already exists!");
System.exit(0);
}
}
fw.append(" " + str);
System.out.println("Entry added!");
fw.close();
System.exit(0);
}
if(choice==2)
{
Path path = Paths.get("masterfile.txt");
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
System.out.println("Enter String you want to modify which is present in text file");
String input = sc.next();
System.out.println("Enter new modified string");
String output = sc.next();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(newTextFile), charset));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
Scanner fileSC = new Scanner(newTextFile);
int count = 0;
while(fileSC.hasNextLine())
{
String line = fileSC.nextLine();
System.out.print("Line " + count + " " );
count++;
if (line.contains(input)){
content = line.replace(input, output);
writer.write(content);
}
}
newTextFile.delete();
temp.renameTo(newTextFile);
System.out.print("Modified");
reader.close();
writer.close();
}
if(choice==3)
{
System.out.println("Enter string to delete");
String delete = sc.next();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(newTextFile), charset));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
Scanner fileSC = new Scanner(newTextFile);
int count =0;
while(fileSC.hasNextLine())
{
String line = fileSC.nextLine();
System.out.print("Line " + count + " " );
count++;
if (line.compareTo(delete)==0)
{
System.out.println("Data already exists!");
line = line.replace(delete, "");
writer.write(line);
}
}
temp.renameTo(newTextFile);
newTextFile.delete();
System.out.println("DELETED");
System.exit(0);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Explanation / Answer
You are missing the lots of code here, as it is truncated at a end of a if(choice==2) block, and a code formatting can use the lots of work. You are IDE can do a formatting for you if you are using one.
while(fileSC.hasNextLine()) {
String line = fileSC.nextLine();
System.out.print("Line " + count + " " );
count++;
line = line.replace(input, output);
bufferedWriter.write(line);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.