?
Solved

Insert an Image into CSV file

Posted on 2010-11-11
16
Medium Priority
?
951 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:rolandmy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34110884
You need to store the path to the image or base64 encode the binary form
0
 
LVL 2

Author Comment

by:rolandmy
ID: 34110929
As stated in the question, I need a sample code as reference.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34110938
What csv are you starting with?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:rolandmy
ID: 34110998
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 34111126
I would strongly advise you to use available csv writers such as ostermiller or open csv
0
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 500 total points
ID: 34111262
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34111296
>>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
 
LVL 10

Expert Comment

by:Hegemon
ID: 34111353
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34111364
Yes, but a byte[] will contain many unprintable characters, and, ahem ... the csv separator ;)
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 34111441
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34111543
>>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
 
LVL 10

Expert Comment

by:Hegemon
ID: 34112983
I wouldn't even store an image in it !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34113454
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
 
LVL 2

Author Comment

by:rolandmy
ID: 34170731
Abandoning this question, haven't found a 100% solution for this question yet. Will split points between Hegemon and CEHJ.
0
 
LVL 2

Author Closing Comment

by:rolandmy
ID: 34170735
Not a 100% solution.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34171570
:)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
A solution for Fortify Path Manipulation.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question