I would like to enter a new item into a CSV file but all my items being entered are all staying on one line in the CSV file. I would like each new item to show up on a new line and I can't seem to find the code to make it work. I've tried the \n and that didn't work.
You probably don’t need to call flush(); if you look here, you will find out why.
There are methods in the BufferedWriter class which can help with new lines. You are better off buffering your FileWriter with a BufferedWriter. There is an example in the BufferedWriter API. BufferedWriter has a write() method which writes a String, but I think you would do better to put all your Strings together with a StringBuilder, and call its toString() method. That is because you get better performance with fewer calls to access the file. Write one line at a time rather than one word at a time.
Please use the code button and indent your code correctly, in which case it will look more like this:-Don't mix tabs and spaces for indentation.
Only use \n if somebody has told you they want the file to contain LF characters.
You will hear us going on about indentation, but it is a very important feature. Incorrect indentation can cause you no end of confusion. correct indentation will, for example allow you to see the start and end of loops. I think you have got confused about where your loops start and finish, and therefore how many lines you are reading and how many of those lines you are discarding without making any use of.
It will also allow you to see code you don't need. Why have you got a loop running exactly once in line 9? And how often will the loop in line 22 run? That's enough to being with.
Are you reading from and writing to the same file? Surely the OS won't allow that? Are you suffering any exceptions?
You would appear to be copying lines from one file to another. You are using "\n" as a delimiter for splitting the input (line 10). But the input will not contain any \n characters in the first place. Your buffered reader will read from the beginning of the line ot the next line p;end sequence, and most OSs in common use nowadays all use line p;end sequences including \n. So there is no need to break with \n.
You haven't shown any exception handling, nor how you are closing the buffered reader, but:-
1: The close() method automatically flushes the stream, so you were right to comment out the flush() call.
2: There is no guarantee that a plain simple close() call will be executed; you should therefore always use try-with-resources, which does guarantee closing the reader/writer.
If you go through the Java™ Tutorials, you will find that you are using old‑fashioned code. I wouldn't use a plain simple FileWriter; I would wrap it in a buffered writer (rather like what you correctly did with the reader):-Line 5 is a combination of reading a line and testing whether it is null; if you want further explanation please ask. Looks at these methods:- Paths#get()Files#newBufferedReader(Path)Files#newBufferedWriter().
That isn't what the file contains; it is what a spreadsheet program displays, which might be different. It is likely the file reads like this:-Note there is a line end sequence at the end of line 2. Also remember never to store passwords in real life; they are too easy to “steal”.