Using both CSVListReader and CSVBeanreader in superCSV - csv

I want to read different CSV files which have all a fixed column number but 2 different files have 2 different column numbers. All the files have a headerline.
So I first use a CSVListReader to get the header and the column numbers and then construct the cell processors and the a CSV BeanReader to map the actual lines to POJO.
I tried first to make it work with passing InputStreamReaders to the superCsv readers constructors and it doesn't work. Works fine however with FileReaders or BufferedReaders.
Is it a bug or it just does not make sense to use InputStremReaders in this situation?
Here is the working code example
CsvListReader listReader = null;
FileReader file = null;
BufferedReader b = null;
try {
file = new FileReader(linkToFile);
b = new BufferedReader(file);
listReader = new CsvListReader(b,
CsvPreference.STANDARD_PREFERENCE);
csvHeader = listReader.getHeader(true);
} catch (IOException e) {
logger.info("Did not manage to get the Csv Header", e);
try {
listReader.close();
file.close();
} catch (IOException e1) {
logger.info("Problem trying to close the readers", e1);
return;
}
}
try {
file = new FileReader(linkToFile);
b = new BufferedReader(file);
beanReader = new CsvBeanReader(b,
CsvPreference.STANDARD_PREFERENCE);
beanReader.getHeader(false);
extractCSV(beanReader, csvHeader);
catch (IOException e) {
logger.info("Did not manage to get a working CsvBeanReader.", e);
try {
beanReader.close();
listReader.close();
file.close();
} catch (IOException e1) {
logger.info("Problem trying to close the readers", e1);
}
return;
}
Thanks in advance

As per Hound Doc Comments, the reason of the mess up was in a bad management of closing the different readers.
Below is the working code using input stream readers
// Reading the Header. A CsvListReader object is used here as it can
// read a variable number of columns in the first line (see
// http://supercsv.sourceforge.net/readers.html)
CsvListReader listReader = null;
InputStreamReader b = null;
try {
b = new InputStreamReader(new BufferedInputStream(new FileInputStream(linkToFile)));
listReader = new CsvListReader(b, CsvPreference.STANDARD_PREFERENCE);
csvHeader = listReader.getHeader(true);
} catch (IOException e) {
logger.info("Did not manage to get the Csv Header", e);
} finally {
try {
listReader.close();
} catch (IOException e1) {
logger.info("Problem trying to close the readers", e1);
return;
}
}
// Using the CSV bean reader to read the file. Now we know the number of
// columns
// A CsvBeanReader object is the choice to extract easier to POJO
// structure
CsvBeanReader beanReader = null;
try {
b = new InputStreamReader(new BufferedInputStream(new FileInputStream(linkToFile)));
beanReader = new CsvBeanReader(b, CsvPreference.STANDARD_PREFERENCE);
// beanReader starts reading from line 2 (see above)
// it is as if we would be reading a file without a header
beanReader.getHeader(false);
extractCSVContacts(beanReader, csvHeader);
} catch (IOException e) {
logger.info("Did not manage to get a working CsvBeanReader.", e);
return;
}
finally {
try {
beanReader.close();
} catch (IOException e1) {
logger.info("Problem trying to close the readers", e1);
}
}

Related

How to format the response from the servlet, so the dojo does not return an exception

According to the dojo tutorial and several examples here, I am trying to send multiple multiple file help. The files to the servlet will arrive in the directory, but the dojo will return the exception
I use dojo 1.10 and javax.servlet.http.HttpServlet v3.0.1
PrintWriter out = response.getWriter();
try {
if (ServletFileUpload.isMultipartContent(request)) {
try {
#SuppressWarnings("unchecked")
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
String name = new File(item.getName()).getName();
item.write(new File("/tmp/eshop/" + File.separator + name));
}
}
out.print("[{uploadresult:'Upload is ok!'}]");
} catch (Exception ex) {
out.print("{uploadresult: 'File upload failed due to : '" + ex+"}");
}
} else {
out.print("{uploadresult:'Sorry this servlet only handles file upload request.'}");
}
out.close();
} catch (Exception e) {
logger.error(e);
}
Error thrown :
/dojo/v1.10/dojox/form/uploader/_HTML5.js:80 Error parsing server
result: SyntaxError: Unexpected token u in JSON at position 2
at JSON.parse ()
at Object.eval (/dojo/v1.10/dojox/form/uploader/_HTML5.js:76)
at XMLHttpRequest. (dojo.js: 15) (anonymous) # /dojo/v1.10/dojox/form/uploader/_HTML5.js:80
/dojo/v1.10/dojox/form/uploader/_HTML5.js:81 [{uploadresult: 'Upload
is ok!'}]
First of all , set the response to json format
add first the response.setContentType("application/json");
And you thing you've to format the json correctly by adding quotes to the key like :
response.setContentType("application/json");
PrintWriter out = response.getWriter();
try {
if (ServletFileUpload.isMultipartContent(request)) {
try {
#SuppressWarnings("unchecked")
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
String name = new File(item.getName()).getName();
item.write(new File("/tmp/eshop/" + File.separator + name));
}
}
out.print("[{'uploadresult':'Upload is ok!'}]");
} catch (Exception ex) {
out.print("{'uploadresult': 'File upload failed due to : '" + ex+"}");
}
} else {
out.print("{'uploadresult':'Sorry this servlet only handles file upload request.'}");
}
out.close();
} catch (Exception e) {
logger.error(e);
}

Javamail - CSVPrinter : send email with csv attached

I want to send an email with csv attachment without having to store the csv file on the server.
I create my csv :
StringWriter sw = new StringWriter();
try {
CSVPrinter csvPrinter = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(
"Année", "Mois", "Date", "Pièce", "Libellé",
"Débit", "Crédit", "Compte", "Journal"
));
for (ExportComptaAV export : exports){
csvPrinter.printRecord(export.getAnnee().getLibelle(),
export.getMois().getLibelle(),
export.getDateMouvement().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
export.getPiece(),
export.getLibelle(),
export.getMontantDebit().toString(),
export.getMontantCredit().toString(),
export.getNumCompte(),
export.getCodeJournal());
}
} catch (IOException e) {
e.printStackTrace();
}
And my email procedure :
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
InternetAddress ad = new InternetAddress("test#gmail.com");
message.addTo(ad);
message.setSubject(sujet);
message.setText(content, isHtml);
String fileName = "csvFile.csv";
message.addAttachment(fileName, new ByteArrayResource(...));
javaMailSender.send(mimeMessage);
} catch (MessagingException mex) {
throw mex;
}
So I would like to convert my CsvPrinter to a ByteArrayresource.
Is there a way to do that?
Get the String from the StringWriter, get the bytes from the String, and use them with ByteArrayResource.

Headers also inserting into database while upload csv file data

Here headers are also inserting into database .here uploading the csv file with comma separated data
string Feedback = string.Empty;
string connString = ConfigurationManager.ConnectionStrings["DataBaseConnectionString"].ConnectionString;
using (MySqlConnection conn = new MySqlConnection(connString))
{
var copy = new MySqlBulkLoader(conn);
conn.Open();
try
{
copy.TableName = "BulkImportDetails";
copy.FileName = fileName;
copy.FieldTerminator = ",";
copy.LineTerminator = #"\n";
copy.Load();
Feedback = "Upload complete";
}
catch (Exception ex)
{
Feedback = ex.Message;
}
finally { conn.Close(); }
}
return Feedback;
Use the NumberOfLinesToSkip property to skip the first line, like so:
copy.NumberOfLinesToSkip = 1;
The use of this property is clearly shown in the documentation for MySQLBulkLoader. You must make a habit of reading the documentation to resolve your queries before you post a question here.

JTabbedPane: how to perform action only on active tab

I would like to save only the text on the currently selected tab(Tab1 one) which I added in the design aspect of netbeans and is separate from my new tab function.
My save function works for all other tabs besides tab 1.
Here is my attempt and getting the save to gather text from tab 1 if tab 1 is selected:
public void Save_As()
{
fileChooserTest.setApproveButtonText("Save");
int actionDialog = fileChooserTest.showOpenDialog(this);
File fileName = new File(fileChooserTest.getSelectedFile() + ".txt" );
try{
if(fileName == null)
return;
BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileChooserTest.getSelectedFile() + ".txt"));
String text = ((JTextArea)TabPane.getSelectedComponent()).getText();
if((TabPane.getTitleAt(TabPane.getSelectedIndex())).equals("Doc1.txt"))
{
bw1.write(this.TextArea.getText());
}
else
{
bw1.write(text);
bw1.close();
}
}
catch (IOException ex) {
}
}
You forget to close the BufferedWriter. just add one line to close it will solve your issue.
To make it much better, you should use try ... catch ... finally, and put the BufferedWriter.close() in the finally section.
if((TabPane.getTitleAt(TabPane.getSelectedIndex())).equals("Doc1.txt"))
{
bw1.write(this.TextArea.getText());
bw1.close();// you need to close it here.
}
else
{
bw1.write(text);
bw1.close();
}

net.rim.device.api.io.file.FileIOException: File system out of resources in blackberry

Below code throws net.rim.device.api.io.file.FileIOException: File system out of resources this exception.
Can anyone tell me how it happens?
public Bitmap loadIconFromSDcard(int index) {
FileConnection fcon = null;
Bitmap icon = null;
InputStream is=null;
try {
fcon = (FileConnection) Connector.open(Shikshapatri.filepath + "i"
+ index + ".jpg", Connector.READ);
if (fcon.exists()) {
byte[] content = new byte[(int) fcon.fileSize()];
int readOffset = 0;
int readBytes = 0;
int bytesToRead = content.length - readOffset;
is = fcon.openInputStream();
while (bytesToRead > 0) {
readBytes = is.read(content, readOffset, bytesToRead);
if (readBytes < 0) {
break;
}
readOffset += readBytes;
bytesToRead -= readBytes;
}
EncodedImage image = EncodedImage.createEncodedImage(content,
0, content.length);
image = resizeImage(image, 360, 450);
icon = image.getBitmap();
}
} catch (Exception e) {
System.out.println("Error:" + e.toString());
} finally {
// Close the connections
try {
if (fcon != null)
fcon.close();
} catch (Exception e) {
}
try {
if (is != null)
is.close();
is = null;
} catch (Exception e) {
}
}
return icon;
}
Thanks in advance...
Check this BB dev forum post - http://supportforums.blackberry.com/t5/Java-Development/File-System-Out-of-Resources/m-p/105597#M11927
Basically you should guaranteedly close all connections/streams as soon as you don't need them, because there is a limited number of connection (be it a file connection or http connection) handles in OS. If you execute several loadIconFromSDcard() calls at the same time (from different threads) consider redesign the code to call them sequentially.
UPDATE:
To avoid errors while reading the content just use the following:
byte[] content = IOUtilities.streamToBytes(is);
And since you don't need file connection and input stream any longer just close them right after reading the content (before creating EncodedImage):
is.close();
is = null; // let the finally block know there is no need to try closing it
fcon.close();
fcon = null; // let the finally block know there is no need to try closing it
Minor points:
Also in the finally block it is worth set fcon = null; explicitly after you close it, I believe this can help old JVMs (BB uses Java 1.3 - rather old one) to decide quicker that the object is ready to be garbage collected.
I also believe that the order you close streams in the finally block may be important - I'd change to close is first and then fcon.