java大作业源代码 java大作业题目有哪些( 五 )


new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream
.write("\"" + (String) propertyEntry.getValue() != null ? (String) propertyEntry
.getValue() : "" + "\"");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
csvFileOutputStream.newLine();
// 写入文件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
/*-------------------------------*/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {
if("证件号码".equals(propertyEntry.getValue())){
//避免:身份证号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()) + "\t");
}else{
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));
}
}
csvFileOutputStream.write(sbContext.toString());
/*-------------------------------*/
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 下载文件
*
* @param response
* @param csvFilePath
*文件路径
* @param fileName
*文件名称
* @throws IOException
*/
public static void exportFile(HttpServletRequest request,
HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer))0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
System.out.println(e1);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/**
* 删除该目录filePath下的所有文件
*
* @param filePath
*文件目录路径
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; 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 (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; i++) {

推荐阅读