1
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.

Sean
  • 31
  • 4
  • try new FileWriter(file) – Komdosh Nov 21 '19 at 16:20
  • But don't I need that true to append? I will try that, thank you. – Sean Nov 21 '19 at 16:22
  • This will print to the first line, but then everything else overwrites that first line. Getting there though. – Sean Nov 21 '19 at 16:37
  • Your code works ok on my machine. What error are you getting? – DodgyCodeException Nov 21 '19 at 16:39
  • I'm not getting any error with the print. It prints to the second line, skips the first line of the csv. But then when I try to read back in that data I get my error. It works perfectly fine if I have a first line inputted manually. Prints to second line then third etc. Just keeps skipping the first line and that breaks my reader. – Sean Nov 21 '19 at 16:40
  • Your code shows simply some lines writing to a text file. But you mention things like CSV, date and reader. What are these? Could you be more specific? – DodgyCodeException Nov 21 '19 at 16:43
  • They're just comma separated values. For reservations. And those I print to the file. It works perfectly well if I already have one inserted manually on the first line of the csv, prints to second, third, fourth etc after, but if I don't it will only print to the second line, skips the first, which breaks my reader. Got no clue why it's doing that honestly. – Sean Nov 21 '19 at 16:46
  • Is the code shown above producing this CSV file, or is this CSV file something else that you're reading from? Also, what do you mean by "skipping the first line?" If you write several lines to a file, there will always be a first line, no matter what you do. – DodgyCodeException Nov 21 '19 at 16:50
  • 1
    You should try to post a [mcve]. In the code you posted, what is `reservation`? – Abra Nov 21 '19 at 16:52
  • I'm both printing and reading from the csv file. Yes, it's producing it. Then it breaks when I'm trying to read from it and there's no first line. – Sean Nov 21 '19 at 16:53
  • So your problem is probably nothing to do with _writing_ the first line, but when you _read_ the file it starts reading from the second line. Maybe you need to post a minimal reproducible example. – DodgyCodeException Nov 21 '19 at 17:00
  • No. I can't write a first line. The code above provides me with this: https://imgur.com/a/150sINj. And that then breaks the program. I mean the program can run fine if i just input a first line in that csv manually. Then all other entries work just perfect. But that first blank line is killing me. Thanks for your patience mate. – Sean Nov 21 '19 at 17:13
  • So I updated above in the main post, but... Anyway thanks folks. – Sean Nov 21 '19 at 17:21
  • Instead of using Excel to see the content of the file, it would be better to examine it in a text editor, especially one where you can configure it to show you all control characters including newlines, so that you can see exactly what the file contains. It looks like there might be a rogue newline at the start for some peculiar reason, but I'm not sure. – DodgyCodeException Nov 22 '19 at 09:28

1 Answers1

0

Try this:-

try (PrintWriter printWriter = new PrintWriter(new FileWriter(file, true))) {
            printWriter.write(reservation.toString());
        } catch (IOException e) {
            logger.error("Error encountered whilst writing the file. Error : ", e);  e.printStackTrace();
        }
v2k
  • 41
  • 2
  • Nope second line. It's driving me mad haha. Thanks anyway buddy. – Sean Nov 21 '19 at 16:36
  • hmm, but then, the same code writes to each line of csv, and i use it extensively in an application of mine. Something else might be the fault then – v2k Nov 21 '19 at 17:12