Although the file that is getting this error is in my java project file, when I use print stack trace I get the java.nio.file.NoSuchFileException error. Does anyone maybe know why?
In my code I am writing to a file named savedata.txt which is the file getting the java.nio.file.NoSuchFileException error. Otherwise my code is also deleting from this file. In order to delete from that file, I have to use a new file, temp.txt, which I write everything except the line that is deleted into. temp.txt is not getting this error
this is done in 3 methods, the same thing in each method
import java.util.*;
import java.util.regex.Pattern;
import java.time.*;
import java.io.*;
import java.text.*;
import java.time.temporal.ChronoUnit;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.FileNotFoundException;
public class input{
public LocalDate date1;
public input (LocalDate date1){
this.date1=date1;
}
public void getSubject(){
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
System.out.println("When you are done entering homework, enter 'done' at subject");
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty", "2024-08-24");
System.out.println("Enter the subject of the homework");
homework1.subject= scanner.nextLine();
if(homework1.subject.equals("done")){
return;
}
writer.println(homework1.subject);
writer.close();
}catch(Exception c){
c.printStackTrace();
System.out.println("did not work");
}
}
public void getDate() throws ParseException{
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
Scanner readlines = new Scanner("savedata.txt");
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty","2024-08-24" );
//LocalDate.now()
System.out.println("Enter the due date of the homework: YYYY-MM-DD");
homework1.dueDate= scanner.nextLine();
//LocalDate.parse
while(homework1.dueDate!="done"){
writer.println(homework1.dueDate);
break;
}
while(homework1.dueDate.equals("done")){
File oldfile = new File("savedata.txt");
File newfile = new File("temp.txt");
int line=0;
String currentline;
FileWriter fw= new FileWriter("temp.txt");
BufferedWriter bw =new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
FileReader fr= new FileReader("savedata.txt");
BufferedReader br = new BufferedReader(fr);
while((currentline=br.readLine())!=null){
line++;
if(count!=line){
pw.println(currentline);
}
}
pw.flush();
pw.close();
br.close();
fw.close();
bw.close();
fr.close();
oldfile.delete();
File dump=new File("savedata.txt");
newfile.renameTo(dump);
break;
}
writer.close();
}catch(Exception c){
}
}
public void getArray(){
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty", "2024-08-24");
System.out.println("Enter parts of homework. Write done when done.");
String finish="";
while(finish.equals("done")){
File oldfile = new File("savedata.txt");
File newfile = new File("temp.txt");
int line=0;
String currentline;
FileWriter fw= new FileWriter("temp.txt");
BufferedWriter bw =new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
FileReader fr= new FileReader("savedata.txt");
BufferedReader br = new BufferedReader(fr);
while((currentline=br.readLine())!=null){
line++;
if((count!=line)|| (count-1!=line)){
pw.println(currentline);
}
}
pw.flush();
pw.close();
br.close();
fw.close();
bw.close();
fr.close();
oldfile.delete();
File dump=new File("savedata.txt");
newfile.renameTo(dump);
break;
}
while(!finish.equals("done")) {
finish= scanner.nextLine();
if(!finish.equals("done")) {
parts newpart = new parts(finish);
homework1.addPart(newpart);
}
}
System.out.println(homework1.part);
writer.println(homework1.part);
writer.close();
}catch(Exception c){
}
}
}```
Natalie Ajmeri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.