java期末作业做什么程序好(需要一份500行的java程序,期末大作业,最好带详细注释。)
大家好,今天小编来为大家解答java期末作业做什么程序好这个问题,需要一份500行的java程序,期末大作业,最好带详细注释。很多人还不知道,现在让我们一起来看看吧!
需要一份500行的java程序,期末大作业,最好带详细注释。
Java生成CSV文件简单操作实例
CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。如:
123 1,张三,男2,李四,男3,小红,女Java生成CSV文件(创建与导出封装类)
package com.yph.omp.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
/**
* 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(!file.exists()){
file.mkdir();
}
//定义文件名格式并创建
csvFile= File.createTempFile(fileName,".csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream= new BufferedWriter(new OutputStreamWriter(
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; i< files.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; i< files.length; 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>();
row1.put("1","11");
row1.put("2","12");
row1.put("3","13");
row1.put("4","14");
exportData.add(row1);
row1= new LinkedHashMap<String, String>();
row1.put("1","21");
row1.put("2","22");
row1.put("3","23");
row1.put("4","24");
exportData.add(row1);
LinkedHashMap map= new LinkedHashMap();
map.put("1","第一列");
map.put("2","第二列");
map.put("3","第三列");
map.put("4","第四列");
String path="d:/export";
String fileName="文件导出";
File file= CSVUtil.createCSVFile(exportData, map, path, fileName);
String fileNameNew= file.getName();
String pathNew= file.getPath();
System.out.println("文件名称:"+ fileNameNew);
System.out.println("文件路径:"+ pathNew);
}
}
//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey())+"\t",只为解决数字格式超过15位后,在Excel中打开展示科学记数问题。
《Java》期末课程设计
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class QuestionOne{
/**
*编程将从键盘输入文本中的子字符串“word”替换为字符串“world”,并删除所有的子字符串“this”。
*序程要求:程序中要包含有注释,对于使用的变量和方法的功能要加以说明。
*/
public static void main(String[] args){
BufferedReader reader= new BufferedReader(new InputStreamReader(
System.in));
String message= null;//存储用户输入的字符串
try{
while((message= reader.readLine())!= null){
//打印处理前的字符串
System.out.println("输入的字符串为:"+ message);
//把 word替换为 world
message= message.replaceAll("word","world");
//把 this替换为空
message= message.replaceAll("this","");
//打印处理后的字符串
System.out.println("处理后为:"+ message);
}
} catch(Exception e){
e.printStackTrace();
System.out.println("出现异常,程序退出!");
}
}
}
急求一个用java web编写的购物车程序,期末考试要用。
查询的资料,找到三种方法:
1.用cookie实现购物车;
2.用session实现购物车;
3.用cookie和数据库(购物车信息持久化)实现购物车;
=========================================================================
分析一下这三种方法的优缺点:
1.单纯有cookie实现购物车,这样的购物车不是很理想,设想一下,如果客户端的浏览器把cookie给禁用了,
这种方法就会在这里流产...
2.session中保存购物车的信息,这个只是在一个会话中可用,如果用户没有登录,或者说登录了以后,添加购物车,在关闭浏览器
或者登出后,之前所添加的购物车通通都流产啦...
其实每次遇到不会的都来求助是很不方便的
如果你想了解和学习更多的JAVA编程,成为一名真正的JAVA高手,你可以来这个裙,前面三个数是四二六中间是三九六后面是二八四
把这串数字连起来就可以了,这是一个高手的学习裙,在这里你可以学习到书上学不到的知识,还有大牛相互讨论并指导你解答哦!
3.我这里要说就是这种方法啦.....
主要的流程:
A.用户登录前的数据流:用户在没有登录系统的时候,对喜欢的商品进行添加购物车,那么这个时候,我们可以把购物车信息保存
到cookie中,这里会涉及到cookie的添加,修改操作;也即如果之前在cookie中不存对应的cookie,则就对cookie进行添加操作。
如果在cookie中存在对应的cookie,那么,这时候,就要对cookie进行修改操作了(这里涉及到用户对同一个商品进行多次添加购物车的情况)。
B.用户登录后的数据流:用户在登录后,系统首先做的第一件事就是去获取对应的cookies,如果存在相关的购物车cookies,那么就对该购物车
信息进行相应用户User的持久化操作,要么添加,要么修改。(添加操作:该用户所对应的购物车如果没有相应的信息进行添加操作;修改操作:类似的,
如果存在对应用户的购物车信息,就进行修改操作)。用户登录后,也可以进行购物车的添加操作,不过,这里不是添加到cookie中,而是直接持久化到
数据库中。注:用户登录后的数据都是和数据库打交道。
===========================================
以上就是我的见解如果有什么不懂可以问我想学习的童鞋秒懂供参考满意请采纳
OK,本文到此结束,希望对大家有所帮助。