• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

output newline to CSV file how to

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 77213
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it works if you use it with FileWriter...It really does!
 
Campbell Ritchie
Marshal
Posts: 77213
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have 2 test case login and home. by using this code login page result id override by home page result .

------------------------CODE----------------------

BufferedReader itemsCSV = new BufferedReader(new FileReader(path));
 
   String dataRow = itemsCSV.readLine();

         
   String[] rows = new String[1];
   String[] dataArray = new String[11];
   System.out.println(i);

     
   while (dataRow != null){
   
    for(int x = 0; x < 1; x++){
   
       dataArray = dataRow.split("\n");
       
       rows[x] = dataArray[0];
       
       dataRow = itemsCSV.readLine(); // Read next line of data.
             }

   }
   
   
     FileWriter writer = new FileWriter(path);
   writer.append("Test Case Description");
   writer.append(',');
   writer.append("Result");
   writer.append('\n');

   for(int p = 0; p<rows.length; p++){
   
     writer.append(rows[p]);
   
   }
   
   writer.append('\n');
   writer.append(testcase);
   writer.append(',');
   writer.append(result);
   writer.append('\n');  

 
//    writer.flush();
   writer.close();
 
Campbell Ritchie
Marshal
Posts: 77213
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Campbell Ritchie
Marshal
Posts: 77213
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
     
    somia sharma
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    IN CSV we were displaying Test case name with is result whether is pass or fail
    in this  format

    t.jpg
    [Thumbnail for t.jpg]
     
    Campbell Ritchie
    Marshal
    Posts: 77213
    370
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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”.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic