Insert an Image into CSV file

I want to write a Java Program to insert an image into CSV file.

Image is stored locally. Any experts able to provide some sample code?
LVL 2
rolandmyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You need to store the path to the image or base64 encode the binary form
0
rolandmyAuthor Commented:
As stated in the question, I need a sample code as reference.
0
CEHJCommented:
What csv are you starting with?
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

rolandmyAuthor Commented:
How do I insert an image into sample.csv?

Code is like below:

java.io.FileOutputStream os = new java.io.FileOutputStream(new java.io.File("C:\\work\\sample.csv"));
    java.io.PrintWriter pw = new java.io.PrintWriter(os, true);    
    String separator = ",";
    String [] content = new String [2];
 
    content[0]= "content 0"+separator+ (
        ("data 01 ")+separator+
        ("data 02 ")+separator+
        ("data 03 ")+separator+
        ("data 04 ")+separator+
        ("data 05 ")+separator
        );
   
    content[1]= "content 1"+separator+ (
        ("rdata 01 ")+separator+
        ("rdata 02 ")+separator+
        ("rdata 03 ")+separator+
        ("rdata 04 ")+separator+
        ("rdata 05 ")+separator
        );
 
    for(int i=0;i<content.length;i++){
     pw.println( content[i]);
     pw.flush();
    }
0
CEHJCommented:
I would strongly advise you to use available csv writers such as ostermiller or open csv
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HegemonCommented:
Just open the file, read its content into byte[] array and output it into a "field" of your CSV between separators, taking care of all characters CSV might not like. This is best done by following CEHJ's advice above.
0
CEHJCommented:
>>read its content into byte[] array

Ooh no. It needs to be base64-encoded if you want to embed it. csv files are text files
0
HegemonCommented:
This article http://creativyst.com/Doc/Articles/CSV/CSV01.htm (see the 1st paragrpah in the CSV & Unicode section) suggests that any ASCII character can be used in CSV. The encoding will be needed only to transfer such a file.
0
CEHJCommented:
Yes, but a byte[] will contain many unprintable characters, and, ahem ... the csv separator ;)
0
HegemonCommented:
There is no requirement for the characters to be printable, it will do if the processing application knows how to handle the raw byte data.
If the fields are enclosed into double quotes, then the content can be anything, including commas, double quotes and new lines - that's where the 3rd party tools you recommended come handy.
0
CEHJCommented:
>>There is no requirement for the characters to be printable

Yes, but why on earth would you want your csv file to be messed up, with line feeds and other  'garbage'?
0
HegemonCommented:
I wouldn't even store an image in it !
0
CEHJCommented:
No - nor would i - that's why i recommended storing a path to the image, but of course rolandmy might not want to be dependent on a particular file system
0
rolandmyAuthor Commented:
Abandoning this question, haven't found a 100% solution for this question yet. Will split points between Hegemon and CEHJ.
0
rolandmyAuthor Commented:
Not a 100% solution.
0
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.