Given an input of a list of beans, it creates a tab separated file the bean attributes being the columns in the tab separated file.
package pkg; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; /** * * This class is used to transform the result set to Java objects which are * formatted for FTP. */ public class FileTransform { private static final String PADDING_STRING = "0"; private static final String BLANK_STRING = " "; private static HashMap colMap; private int maxColCount = 3;/* * this is the number of columns to be printed * in one row.One can set this in any properties * file. or pass the colcount of resultset as a * parameter. */ private String filepath = "c:\\temp.txt";//The file path can be read from // properties file. public FileTransform() { super(); } /** * This method creates a file with tab separated data using result set * passed to it... * * @param ResultSet * @return Void * */ public void getTabSeparatedFile(ArrayList list) { File outFile = new File(filepath); FileOutputStream out; try { out = new FileOutputStream(outFile); String prnt_string = ""; int k = 0; int l = 0; int size = list.size(); int count = size; Iterator it = list.iterator(); while (size >= 0) { for (k = 0; k < 3; k++) { if (l >= count) { break; } prnt_string = getValue(list.get(l), prnt_string); l++; size--; } prnt_string = prnt_string + "\r\n"; out.write(prnt_string.getBytes()); prnt_string = ""; if (l >= count) { break; } } out.close(); } catch (FileNotFoundException fnfEx) { fnfEx.printStackTrace(); } catch (IOException ioEx) { ioEx.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * This method is used to get the formated value out of the object obtained * from db.. * * @param String,Object,String * @return String * */ public String getValue(Object value, String prnt_string) { String formatedValue = ""; if (value != null && value.toString().trim().length() != 0) { String valStrng = value.toString().trim(); formatedValue = valStrng; } else { formatedValue = BLANK_STRING; } prnt_string = prnt_string + formatedValue + "\t"; return prnt_string; } /** * This method gets a resultset and adds its data to an arraylist. returns * the arraylist; * * @param resultset * @return arraylist */ public ArrayList getListfromResultset(ResultSet rs) { ArrayList list = new ArrayList(); ResultSetMetaData metadata = null; int col = 0; //To be used if the array list is constructed from a Resultset. try { metadata = rs.getMetaData(); col = metadata.getColumnCount(); while (rs.next()) { for (int k = 1; k <= col; k++) { Object obj = rs.getObject(k); list.add(obj); } } } catch (SQLException e) { e.printStackTrace(); } return list; } }