Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:49:34+00:00 2026-06-11T20:49:34+00:00

I was searching for an answer for this but I didn’t find it. Does

  • 0

I was searching for an answer for this but I didn’t find it. Does anyone have a solution for this kind of problem. I have a set of text variables that I have to write into the .CSV file using Java. I am currently doing a project with JavaScript that calls for Java. This is a function that I have right now that does the job well and writes the text into .CSV line by line.

function writeFile(filename, data)
{
   try
   { 

      //write the data

      out = new java.io.BufferedWriter(new java.io.FileWriter(filename, true));
      out.newLine();
      out.write(data);
      out.close();
      out=null;
   }
   catch(e)   //catch and report any errors
   {
      alert(""+e);
   }
}

But now I have to write parts of text one by one like the example bellow.

first0,second0,third0
first1,second1,third1
first2,second2,third2
.
.
.
first9,second9,third9

So the algorithm goes like this. The function writes first0 with comma then goes to the next line writes first1, goes to next line writes first2 and so one until first9. After that part is done the script goes to the beginning of the file and writes second0 behind the comma, goes to the next line and writes second1 behind the comma and so on. You get the idea.

So now I need java

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:49:35+00:00Added an answer on June 11, 2026 at 8:49 pm

    You might want to consider using Super CSV to write the CSV file. As well as taking care of escaping embedded double-quotes and commas, it offers a range of writing implementations that write from arrays/Lists, Maps or even POJOs, which means you can easily try out your ideas.

    If you wanted to keep it really simple, you can assemble your CSV file in a two-dimensional array. This allows to to assemble it column-first, and then write the whole thing to CSV when it’s ready.

    package example;
    
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.supercsv.io.CsvListWriter;
    import org.supercsv.io.ICsvListWriter;
    import org.supercsv.prefs.CsvPreference;
    
    public class ColumnFirst {
    
        public static void main(String[] args) {
    
            // you can assemble this 2D array however you want
            final String[][] csvMatrix = new String[3][3];
            csvMatrix[0][0] = "first0";
            csvMatrix[0][1] = "second0";
            csvMatrix[0][2] = "third0";
            csvMatrix[1][0] = "first1";
            csvMatrix[1][1] = "second1";
            csvMatrix[1][2] = "third1";
            csvMatrix[2][0] = "first2";
            csvMatrix[2][1] = "second2";
            csvMatrix[2][2] = "third2";
    
            writeCsv(csvMatrix);
    
        }
    
        private static void writeCsv(String[][] csvMatrix) {
    
            ICsvListWriter csvWriter = null;
            try {
                csvWriter = new CsvListWriter(new FileWriter("out.csv"), 
                    CsvPreference.STANDARD_PREFERENCE);
    
                for (int i = 0; i < csvMatrix.length; i++) {
                    csvWriter.write(csvMatrix[i]);
                }
    
            } catch (IOException e) {
                e.printStackTrace(); // TODO handle exception properly
            } finally {
                try {
                    csvWriter.close();
                } catch (IOException e) {
                }
            }
    
        }
    
    }
    

    Output:

    first0,second0,third0
    first1,second1,third1
    first2,second2,third2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem :) I was searching the web but didn't find
I have been searching for an answer to this question but cannot find anything
I've been searching the answer but didn't find any information about this function except
I have been searching for an answer to this but can't find it on
I have been searching for an answer to this but cannot find a simple
Been searching all over for this but cant find an answer so thought I
I've tried searching for an answer to this problem, but I'm not entirely sure
Ok, I have tried searching around for this answer, but with no luck. I
I have been searching for an answer to this dilemma but found nothing. I
I have been searching for an answer to this for two days now, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.