Solved

Insert an Image into CSV file

Posted on 2010-11-11
16
936 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
  • 8
  • 4
  • 4
16 Comments
 
LVL 86

Expert Comment

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

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
What csv are you starting with?
0
 
LVL 2

Author Comment

by:rolandmy
Comment Utility
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 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 86

Expert Comment

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

Expert Comment

by:Hegemon
Comment Utility
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
Comment Utility
>>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
Comment Utility
I wouldn't even store an image in it !
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
Not a 100% solution.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
array6 challenfge 6 62
ArrayIndexOutOfBoundException 9 30
eclipse java  build path 6 17
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now