1 7 package org.ejtools.util.export; 8 9 import java.io.File ; 10 import java.io.FileWriter ; 11 import java.io.IOException ; 12 13 import javax.swing.JFileChooser ; 14 import javax.swing.filechooser.FileFilter ; 15 16 import org.apache.log4j.Logger; 17 18 19 25 public class CSVTools 26 { 27 28 private static FileFilter CSV_FILE_FILTER = 29 new FileFilter () 30 { 31 public boolean accept(File file) 32 { 33 return file.getName().endsWith(".csv"); 34 } 35 36 37 public String getDescription() 38 { 39 return "Comma Separated Values file (*.csv)"; 40 } 41 }; 42 43 private static Logger logger = Logger.getLogger(CSVTools.class); 44 45 46 47 private CSVTools() 48 { 49 super(); 50 } 51 52 53 59 public static void exportAsCVS(StringBuffer content, File output) 60 { 61 try 62 { 63 FileWriter writer = new FileWriter (output); 64 writer.write(content.toString()); 65 writer.flush(); 66 writer.close(); 67 } 68 catch (IOException ioe) 69 { 70 logger.error("Can't export content as CSV", ioe); 71 } 72 } 73 74 75 80 public static File selectCSVFile() 81 { 82 SecurityManager s = System.getSecurityManager(); 84 System.setSecurityManager(null); 85 86 JFileChooser chooser = new JFileChooser (System.getProperty("user.dir")); 88 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 89 chooser.setFileFilter(CSV_FILE_FILTER); 90 int returnVal = chooser.showSaveDialog(null); 91 System.setSecurityManager(s); 92 if (returnVal != JFileChooser.APPROVE_OPTION) 93 { 94 return null; 95 } 96 return chooser.getSelectedFile(); 97 } 98 } 99 | Popular Tags |