0

I have an ArrayList (actually an ArrayList of ArrayLists), each with ints, doubles, and Strings. I would like to write these to a csv file.

It seems that PrintWriter requires the data to be a CharSequence. Alternatively, when I try to use the write() function of BufferedWriter, it requires that I specify the type, e.g. int or String.

Is there a way I can write different types to a csv file? If possible, I would prefer to avoid 3rd-party utilities.

3 Answers 3

1

A CSV file doesn't care what the value types are, so you should just write all of your data as strings. For the non-string values, this is as simple as

printWriter.write("" + <integer or float variable>);
Sign up to request clarification or add additional context in comments.

Comments

1

You need to convert each type that you want to output into a String representation before hand. If it is a class of your own you are trying to convert to a String create a public String toString() method for it which will automatically be called when appending to a string or passing to anything that requires a String.

Comments

0

Use String.valueof(elementList) before writing to convert each value in String ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.