Pages

Tampilkan postingan dengan label files. Tampilkan semua postingan
Tampilkan postingan dengan label files. Tampilkan semua postingan

Rabu, 12 Februari 2014

Java OpenCSV tutorial to work with CSV files



BeanIO is an open source Java framework for marshaling and marshaling Java beans from a flat file, stream, or simple String object. It is very powerful with support for XML, CSV, delimited and fixed length stream formats, Object binding, filed validation, integration with spring-batch, etc. But there are scenarios where you need to simply read or write CSV data, OpenCSV framework is quite useful. Here are simple step by step guide to convert Java objects to CSV data.


Step 1: Define in the maven pom.xml file to bring in the OpenCSV jar file.

 <properties>
<opencsv.version>2.3</opencsv.version>
</properties>

<dependencies>
<!-- Open CSV -->
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
<dependencies>


Step 2: Write the Person model class.

package com.mycompany.app2;

public class Person
{
private String firstName;
private String surname;
private int age;

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getSurname()
{
return surname;
}

public void setSurname(String surname)
{
this.surname = surname;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

}



Step 3: Write the interface.

package com.mycompany.app2;

import java.util.List;

public interface SimpleWriter
{
abstract String writeCSV(List persons);
}



Step 4: Write the implementation using the above interface.

package com.mycompany.app2;

import au.com.bytecode.opencsv.CSVWriter;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SimpleWriterImpl implements SimpleWriter
{

private static final char DELIMITER = ,;

public String writeCSV(List<Person> persons)
{
StringWriter writer = new StringWriter();
CSVWriter csvWriter = null;

try
{
csvWriter = new CSVWriter(writer, DELIMITER, CSVWriter.DEFAULT_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER, "
");

List<String[]> records = new ArrayList<String[]>(10);

for (Person person : persons)
{
List<String> record = Arrays.asList(String.valueOf(person.getFirstName()),
String.valueOf(person.getSurname()),
String.valueOf(person.getAge()));

String[] recordArray = new String[record.size()];
record.toArray(recordArray);
records.add(recordArray);
}

csvWriter.writeAll(records);

}

catch (Exception e)
{
e.printStackTrace();
}

return writer.toString();
}

}



If you want to write to a file, you can use a FileWriter instead of the StringWriter.

Step 5: Write a test class to test it.In practice, a unit test class must be written.

package com.mycompany.app2;

import java.util.ArrayList;
import java.util.List;

public class SimpleWriterTest
{
public static void main(String[] args)
{
Person p1 = new Person();
p1.setFirstName("John");
p1.setSurname("Smith");
p1.setAge(12);
List persons = new ArrayList(5);
persons.add(p1);

SimpleWriter sw = new SimpleWriterImpl();
String csvOutPut = sw.writeCSV(persons);
System.out.println(csvOutPut);

}
}

Step 6: The output is

"John","Smith","12"

Read More..

Sabtu, 08 Februari 2014

How to Generate MD5 checksum for Files in Java

MD5 checksums are good to verify integrity of files and It's easy to generate MD5 checksum in Java. Java provides couple of ways to generate MD5 checksum for any file, you can either use java.security.MessageDigest or any open source library like Apache commons codec or Spring. All 3 ways we have seen in our earlier article about generating MD5 hash for Stringis also applicable to generate MD5  checksum for any file. Since most of md5() or md5Hex() method takes byte[], you can simply read bytes from InputStream or pass to these md5 methods. Apache commons codec from version 1.4 also provides an overloaded method to accept InputStream, which makes generating checksum very easy in Java. For those who are not familiar with checksum, it's a fixed size datum generated from a block of data to detect any accidental change in data. Which means once you create checksum for a file, which is based on contents of file, any change on file e.g. adding white space, deleting a character will result in different checksum. By comparing stored checksum with current checksum, you can detect any change on File. It's good practice to provide checksum of WAR or JAR files to support teams for production release. In this Java tutorial we will learn how to create MD5 checksum for any file in Java.
Read more »
Read More..