A. csv file
Comma-separated values (comma-separated values,csv, sometimes referred to as character-delimited values, because delimited characters can also be not commas), whose files store tabular data (numbers and text) in plain text. Plain text means that the file is a sequence of characters and does not contain data that must be interpreted like a binary number. A CSV file consists of any number of records separated by a newline character, each record consists of a field, and the delimiter between the fields is another character or string, most commonly a comma or tab. Typically, all records have exactly the same sequence of fields.
CSV is a common, relatively simple file format that is widely used by users, businesses, and science. The most widespread application is the transfer of tabular data between programs, which themselves operate in incompatible formats (often in private and/or non-canonical formats). Because a large number of programs support some kind of CSV variant, at least as an optional input/output format.
"CSV" refers to any file that has the following characteristics:
- Plain text, using a character set such as ASCII, Unicode, EBCDIC, or GB2312;
- Consists of records (typically one record per line);
- Each record is delimited by a delimiter as a field (a typical delimiter has commas, semicolons, or tabs; sometimes separators can include optional spaces);
- Each record has the same field sequence.
Ii. examples
The relevant code is as follows:
1 PackageCom.test.excel;2 3 ImportJava.io.BufferedWriter;4 ImportJava.io.File;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.OutputStreamWriter;8 ImportJava.net.URLEncoder;9 Importjava.util.ArrayList;Ten Importjava.util.Arrays; One Importjava.util.Date; A Importjava.util.List; - - Importorg.junit.Test; the - /** - * Create a CSV file - */ + Public classCsvcrate { - + /** A * Create a CSV file at */ - @Test - Public voidcreatecsv () { - - //Table Header -Object[] head = {"Customer name", "Voucher Type", "date", }; inList<object> headlist =Arrays.aslist (head); - to //Data +List<list<object>> dataList =NewArraylist<list<object>>(); -List<object> rowlist =NULL; the for(inti = 0; I < 100; i++) { *Rowlist =NewArraylist<object>(); $Rowlist.add ("Zhang San" +i);Panax NotoginsengRowlist.add ("263834194" +i); -Rowlist.add (NewDate ()); the Datalist.add (rowlist); + } A theString fileName = "Testcsv.csv";//file name +String FilePath = "c:/test/";//file path - $File CSVFile =NULL; $BufferedWriter Csvwtriter =NULL; - Try { -CSVFile =NewFile (FilePath +fileName); theFile parent =csvfile.getparentfile (); - if(Parent! =NULL&&!parent.exists ()) {Wuyi parent.mkdirs (); the } - csvfile.createnewfile (); Wu - //GB2312 make the delimiter read correctly "," AboutCsvwtriter =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (CSVFile), "GB2312"), 1024); $ - //to download the file, use the following code - //Response.setcontenttype ("application/csv;charset=gb18030"); - //Response.setheader ("content-disposition", "attachment; Filename= "+ urlencoder.encode (fileName," UTF-8 ")); A //Servletoutputstream out = Response.getoutputstream (); + //csvwtriter = new BufferedWriter (new OutputStreamWriter (out, "GB2312"), 1024x768); the - intnum = Headlist.size ()/2; $StringBuffer buffer =NewStringBuffer (); the for(inti = 0; i < num; i++) { theBuffer.append (","); the } theCsvwtriter.write (buffer.tostring () + FileName +buffer.tostring ()); - csvwtriter.newline (); in the //write to file header the Writerow (headlist, csvwtriter); About the //Write file Contents the for(list<object>row:datalist) { the writerow (row, csvwtriter); + } - Csvwtriter.flush (); the}Catch(Exception e) {Bayi e.printstacktrace (); the}finally { the Try { - csvwtriter.close (); -}Catch(IOException e) { the e.printstacktrace (); the } the } the } - the /** the * Write a row of data the * @paramRow Data List94 * @paramCsvwriter the * @throwsIOException the */ the Private Static voidWriterow (list<object> row, BufferedWriter csvwriter)throwsIOException {98 for(Object data:row) { AboutStringBuffer SB =NewStringBuffer (); -String rowstr = sb.append ("\" "). Append (data). Append (" \ ","). toString ();101 Csvwriter.write (ROWSTR);102 }103 csvwriter.newline ();104 } the}
Java generates a CSV file