try{
Writer rwrite = new BufferedWriter(new FileWriter(file, true));
rwrite.append(reservation.toString() + "\n");
rwrite.close();
}
catch(IOException e){
System.out.println("Could not open BufferedWriter.");
e.printStackTrace();
}
I mean basically my problem is I can't write to the first line of an empty csv file. I can write to the second just fine. That is, if I've inputted the first line myself manually I can write to the second.
Any help appreciated folks, really, cheers. I'm stumped.
Update:
try{
File file = new File("reservations.csv");
Scanner fileIn = new Scanner(file);
try {
if(fileIn.hasNext()){
PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv", true));
printWriter.write(reservation.toString() + "\n");
printWriter.close();
}
if(!fileIn.hasNext()){
PrintWriter printWriter = new PrintWriter(new FileWriter("reservations.csv"));
printWriter.write(reservation.toString() + "\n");
printWriter.close();
}
} catch (IOException e) {
System.out.println("Could not open BufferedWriter.");
e.printStackTrace();
}
}
catch(IOException e){
System.out.println("Error: failed to read from: " + "reservations.csv");
e.printStackTrace();
}
I changed my code to this, and this works, but looks a small little bit of a monstrosity to me haha. Thanks for any help anyway, and will still be reading.