
Java生成CSV文件简单 *** 作实例
CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:
123 1,张三,男2,李四,男3,小红,女Java生成CSV文件(创建与导出封装类)
package comyphompcommonutil;
import javaioBufferedWriter;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
import javaioOutputStreamWriter;
import javautilArrayList;
import javautilIterator;
import javautilLinkedHashMap;
import javautilList;
import javautilMap;
import javaxservlet>
import javaxservlet>
import orgapachecommonsbeanutilsBeanUtils;
import orgjunitTest;
/
Java生成CSV文件
/
public class CSVUtil {
/
生成为CVS文件
@param exportData
源数据List
@param map
csv文件的列表头map
@param outPutPath
文件路径
@param fileName
文件名称
@return
/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map,
String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!fileexists()) {
filemkdir();
}
// 定义文件名格式并创建
csvFile = FilecreateTempFile(fileName, "csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator
hasNext();) {
javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator
next();
csvFileOutputStream
write("\"" + (String) propertyEntrygetValue() != null (String) propertyEntry
getValue() : "" + "\"");
if (propertyIteratorhasNext()) {
csvFileOutputStreamwrite(",");
}
}
csvFileOutputStreamnewLine();
// 写入文件内容
for (Iterator iterator = exportDataiterator(); iteratorhasNext();) {
Object row = (Object) iteratornext();
for (Iterator propertyIterator = mapentrySet()iterator(); propertyIterator
hasNext();) {
javautilMapEntry propertyEntry = (javautilMapEntry) propertyIterator
next();
/-------------------------------/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtilsgetProperty(row,(String) propertyEntrygetKey())) {
if("证件号码"equals(propertyEntrygetValue())){
//避免:身份z号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t");
}else{
sbContextappend(BeanUtilsgetProperty(row,(String) propertyEntrygetKey()));
}
}
csvFileOutputStreamwrite(sbContexttoString());
/-------------------------------/
if (propertyIteratorhasNext()) {
csvFileOutputStreamwrite(",");
}
}
if (iteratorhasNext()) {
csvFileOutputStreamnewLine();
}
}
csvFileOutputStreamflush();
} catch (Exception e) {
eprintStackTrace();
} finally {
try {
csvFileOutputStreamclose();
} catch (IOException e) {
eprintStackTrace();
}
}
return csvFile;
}
/
下载文件
@param response
@param csvFilePath
文件路径
@param fileName
文件名称
@throws IOException
/
public static void exportFile(>
>
throws IOException {
responsesetCharacterEncoding("UTF-8");
responsesetContentType("application/csv;charset=GBK");
responsesetHeader("Content-Disposition", "attachment; filename="
+ new String(fileNamegetBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = responsegetOutputStream();
while ((len = inread(buffer)) > 0) {
outwrite(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
Systemoutprintln(e1);
} finally {
if (in != null) {
try {
inclose();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/
删除该目录filePath下的所有文件
@param filePath
文件目录路径
/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (fileexists()) {
File[] files = filelistFiles();
for (int i = 0; i < fileslength; i++) {
if (files[i]isFile()) {
files[i]delete();
}
}
}
}
/
删除单个文件
@param filePath
文件目录路径
@param fileName
文件名称
/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (fileexists()) {
File[] files = filelistFiles();
for (int i = 0; i < fileslength; i++) {
if (files[i]isFile()) {
if (files[i]getName()equals(fileName)) {
files[i]delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = new ArrayList<Map>();
Map row1 = new LinkedHashMap<String, String>();
row1put("1", "11");
row1put("2", "12");
row1put("3", "13");
row1put("4", "14");
exportDataadd(row1);
row1 = new LinkedHashMap<String, String>();
row1put("1", "21");
row1put("2", "22");
row1put("3", "23");
row1put("4", "24");
exportDataadd(row1);
LinkedHashMap map = new LinkedHashMap();
mapput("1", "第一列");
mapput("2", "第二列");
mapput("3", "第三列");
mapput("4", "第四列");
String path = "d:/export";
String fileName = "文件导出";
File file = CSVUtilcreateCSVFile(exportData, map, path, fileName);
String fileNameNew = filegetName();
String pathNew = filegetPath();
Systemoutprintln("文件名称:" + fileNameNew );
Systemoutprintln("文件路径:" + pathNew );
}
}
//注:BeanUtilsgetProperty(row,(String) propertyEntrygetKey()) + "\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。
int main()
{
Link head; //链表(不带头节点)
int n;
printf("输入链表的长度n: ");
scanf("%d",&n);
printf("连续输入%d个数据(以空格隔开): ",n);
head=CreateLink(n);
printf("\n原本链表的节点是: ");
DispLink(head);
LinkSort(head);
printf("\n从大到小排序之后: ");
DispLink(head);
printf("\n");
return 0;
}
链表的具体存储表示为:
① 用一组任意的存储单元来存放线性表的结点(这组存储单元既可以是连续的,也可以是不连续的)
② 链表中结点的逻辑次序和物理次序不一定相同。为了能正确表示结点间的逻辑关系,在存储每个结点值的同时,还必须存储指示其后继结点的地址(或位置)信息(称为指针(pointer)或链(link))
链式存储是最常用的存储方式之一,它不仅可用来表示线性表,而且可用来表示各种非线性的数据结构。
百度百科-单链表
#include<iostream>
using namespace std;
struct Linklist
{
int data;
Linklist next;
};
Linklist L,rear;
void create()//尾插法建立链表
{
Linklist p;
int x;
cout<<"输入链表元素(按升序顺序输入):"
while(cin>>x&&x!=0)
{
if(L==NULL)
{
L=new Linklist;
L->data=x;
rear=L;
L->next=NULL;
}
else
{
p=new Linklist;
p->data=x;
rear->next=p;
p->next=NULL;
rear=p;
}
}
rear->next=NULL;
}
void del(int x)//删除data域值为x的结点
{
Linklist p,k,t;
k=p=L;
while(p!=NULL)
{
if(p->data==x)
break;
k=p;
p=p->next;
}
if(p==L)
{
p=L;
L=L->next;
delete p;
}
else if(p==rear)
{
k->next=NULL;
delete p;
}
else if(p==NULL)
{
cout<<"没有值为"<<x<<"的结点 "<<endl;
}
else
{
k->next=p->next;
delete p;
}
}
void insert(int x)//插入data域值为x的结点(默认原链表为升序排列)
{
Linklist s,p,k;
s=new Linklist;
s->data=x;
s->next=NULL;
k=p=L;
while(p!=NULL)
{
if(p->data>=x)
break;
k=p;
p=p->next;
}
if(p==L)
{
L=s;
s->next=p;
}
else if(p==NULL)
{
k->next=s;
}
else
{
k->next=s;
s->next=p;
}
}
void output()//将链表输出
{
Linklist p;
p=L;
cout<<"链表中的元素为:";
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl<<endl;
}
void LinkListDemo()
{ // L是无头结点的单链表
Linklist q, p,t;//p指针用于指示原链表最后的结点(即an),q指针用于指示原来的头结点(即a1)
q=L;
if ( q && q->next )
{
t=q;//将q指针指向a1
L=L->next;//将L指针指向新链表的头结点(即a2)
rear->next=q;
q->next=NULL;
t=NULL;
}
}
void main()
{
create();
output();
int x;
cout<<"输入需要插入的元素:";
cin>>x;
insert(x);
output();
cout<<"输入需要删除的元素:";
cin>>x;
del(x);
output();
LinkListDemo();
output();
}
以上就是关于需要一份500行的java程序,期末大作业,最好带详细注释。全部的内容,包括:需要一份500行的java程序,期末大作业,最好带详细注释。、编写程序,建立一个带有节点的单向链表,输入字符串,并按从小到大顺序组织到链表中、十万火急,尽快。数据结构编程 ,单链表的第一个结点从表头移到表尾等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)