Results 1 to 2 of 2
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    65
    Rep Power
    0

    Default Write a String Variable To CSV

    I am sure this is basic but it's been close to a decade since I've worked with Java.

    Java Code:
        for(String temp : result){
            System.out.println(temp);
        }
    how can I write all the values in temp to a csv file? I'd like just one column of data in the csv file.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Write a String Variable To CSV

    The simplest option is:
    Java Code:
    Files.write(Paths.get("/some/path/file.csv"), result, StandardOpenOption.WRITE);
    Other options are using a BufferedWriter and dump it line by line using the same loop you already have:
    Java Code:
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("/some/path/file.csv"), StandardOpenOption.WRITE)) {
       for(String temp : result){
          writer.write(temp);
          writer.newLine();
       }
    }
    catch (IOException e) {
        //handle this!
    }
    But if you check the source code for Files.write(), you will find that it is exactly the same as the second example.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 2
    Last Post: 08-26-2013, 12:32 AM
  2. String to Variable Name?
    By Prime624 in forum New To Java
    Replies: 3
    Last Post: 02-08-2013, 08:58 AM
  3. write a selection sort without having numerous variable?
    By seandingobat in forum New To Java
    Replies: 6
    Last Post: 10-28-2008, 03:33 PM
  4. Replies: 1
    Last Post: 08-03-2007, 11:52 AM
  5. String Variable
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 06-06-2007, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •