To read from a database, you need JDBC. Please read up on how to connect to a database and perform query etc. you can find examples at javasoft. Formatting would be write each of the relevant columns or any computation performed into a StringBuffer appending commas after every column. stringbuffer sb = new stringbuffer(); To write to a file, use outputstream. Fileoutputstream f = Fileoutstream(filename). bufferedwriter b = new bufferedwriter(new outputstreamwriter(f)); b.writeline(sb.toString()); watch all the cases(lower and upper), i am writing this more as a draft.