中等的I/O读写技术,与反射、配置文件结合
页面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 7 <title>利用反射的io-1</title> 8 9 <meta http-equiv="pragma" content="no-cache"> 10 <meta http-equiv="cache-control" content="no-cache"> 11 <meta http-equiv="expires" content="0"> 12 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 13 <meta http-equiv="description" content="This is my page"> 14 <!-- 15 <link rel="stylesheet" type="text/css" href="styles.css"> 16 --> 17 <script type="text/javascript" src ="${pageContext.request.contextPath }/js/jquery-1.11.1.js"></script> 18 <script type="text/javascript"> 19 // 1.创建一个xmlHttpRequest对象 20 var xmlHttpRequest = null; 21 // 2.判断浏览器类型 22 function getXmlHttpRequest() { 23 // 如果非IE 24 if(window.XMLHttpRequest) { 25 xmlHttpRequest = new XMLHttpRequest(); 26 } else if(window.ActiveXObject){ 27 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 28 } 29 } 30 //ajax请求 31 function ajaxRequest(url, cb, clz, fileName) { 32 // 3.获得浏览器类型 33 getXmlHttpRequest(); 34 if(xmlHttpRequest) { 35 //4. 设置提交方式 36 xmlHttpRequest.open("post", url, true); 37 //5. 设置回调函数 38 xmlHttpRequest.onreadystatechange = cb; 39 // 用POST方式请求,必须加上头文件 40 xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 41 //6. 正式发送请求(发送多个值("clz=" + clz + "&fileName=" + fileName)用‘+’号连接,第二个键前加‘&’符) 42 xmlHttpRequest.send("clz=" + clz + "&fileName=" + fileName); 43 } 44 } 45 //回调函数 46 function cb1 () { 47 if(xmlHttpRequest.readyState == 4 && 48 xmlHttpRequest.status == 200) { 49 // 7.获取响应返回的结果 50 var result = xmlHttpRequest.responseText; 51 alert(result); 52 } 53 } 54 55 function cb2 () { 56 if(xmlHttpRequest.readyState == 4 && 57 xmlHttpRequest.status == 200) { 58 // 7.获取响应返回的结果 59 var result = xmlHttpRequest.responseText; 60 alert(result); 61 // 8.通过eval("")函数将返回结果进行装载(注意eval("")的用法,里面必须用()括起来result = eval("("+result+")");) 62 result = eval("("+result+")"); 63 for(var v in result) { //Js中的foreach,v为索引 64 alert(result[v]); 65 } 66 } 67 } 68 $(function(){ 69 $("#InputEmpbtn").click(function(){ 70 // ( url,回调函数,反射需要的包类驱动,要加载的配置文件) 71 ajaxRequest("../Jxl02Servlet",cb1,"entity.Emp","/user.properties"); 72 }); 73 $("#ExputEmpbtn").click(function(){ 74 ajaxRequest("../OJxl01Servlet",cb2,"entity.Emp"); 75 }); 76 }) 77 </script> 78 </head> 79 80 <body> 81 <input id="InputEmpbtn" type="button" value="员工导入" /> 82 <input id="ExputEmpbtn" type="button" value="员工导出" /> 83 </body> 84 </html>
servlet
1 public void doPost(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 4 try{ 5 response.setContentType("text/html"); 6 PrintWriter out = response.getWriter(); 7 // 获得‘包类驱动’ 8 String clzStr = request.getParameter("clz"); 9 // 获得配置文件信息 10 String fileName = request.getParameter("fileName"); 11 /*********************************************反射开始***********************************************/ 12 //1. 根据’包类驱动‘创建一个类 13 Class<?> c = Class.forName(clzStr); 14 //2. 根据1类创建一个对象 15 Object obj = c.newInstance(); 16 //3. 创建一个集合得到2对象的所有属性 17 Field[] fields = obj.getClass().getDeclaredFields(); 18 //4. 创建配置文件信息的对象 19 ReaderProperties reader = new ReaderProperties(); 20 for (Field field : fields) { 21 field.setAccessible(true); 22 String name = field.getName(); 23 // 配置文件----- 24 Properties properties =reader.loadProperties(fileName); 25 Iterator<Entry<Object, Object>> it = properties.entrySet().iterator(); 26 while (it.hasNext()) { 27 Entry<Object, Object> entry = it.next(); 28 Object key = entry.getKey(); 29 Object value = entry.getValue(); 30 if(name.equals(key)) { 31 // 判断字段类型并赋值 32 if(field.getType().toString().equals("double")) { 33 field.set(obj, Double.valueOf(value.toString())); 34 } else { 35 field.set(obj, value.toString()); 36 } 37 } 38 } 39 } 40 reflectObj(obj); 41 } catch(Exception e){ 42 e.printStackTrace(); 43 } 44 45 } 46 47 private void reflectObj(Object obj) throws Exception { 48 File file = new File("d:/obj.xls"); 49 WritableWorkbook book = Workbook.createWorkbook(file); 50 WritableSheet sheet = book.createSheet("第一个", 0); 51 Field[] fields = obj.getClass().getDeclaredFields(); 52 for (int i = 0; i < fields.length; i++) { 53 Field field = fields[i]; 54 field.setAccessible(true); 55 String name = field.getName(); 56 String v = field.get(obj) == null ? "" : field.get(obj).toString(); 57 Label label1 = new Label(i,0,name); 58 Label label2 = new Label(i,1,v); 59 sheet.addCell(label1); 60 sheet.addCell(label2); 61 } 62 book.write(); 63 book.close(); 64 }
配置文件
1.user.properties (注意:后缀名一定要写正确 .properties)
1 empNo=9527 2 empName=\u5F20\u4E09 3 salary=10000
2.dept.properties
1 deptNo=10 2 deptName=\u8D22\u52A1\u90E8
1 package test; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Enumeration; 6 import java.util.Iterator; 7 import java.util.Properties; 8 import java.util.Map.Entry; 9 10 public class ReaderProperties { 11 12 public static void main(String[] args) { 13 Properties properties = new ReaderProperties().loadProperties("/jdbc.properties"); 14 Enumeration<?> enu = properties.propertyNames(); 15 while (enu.hasMoreElements()) { 16 Object key = enu.nextElement(); 17 System.out.println(key); 18 } 19 Enumeration<Object> enu2 = properties.elements(); 20 while (enu2.hasMoreElements()) { 21 Object value = enu2.nextElement(); 22 System.out.println(value); 23 } 24 25 Iterator<Entry<Object, Object>> it = properties.entrySet().iterator(); 26 while (it.hasNext()) { 27 Entry<Object, Object> entry = it.next(); 28 Object key = entry.getKey(); 29 Object value = entry.getValue(); 30 System.out.println("key :" + key); 31 System.out.println("value :" + value); 32 System.out.println("---------------"); 33 } 34 } 35 36 37 public Properties loadProperties(String resources) { 38 // 使用InputStream得到一个资源文件 39 InputStream inputstream = this.getClass() 40 .getResourceAsStream(resources); 41 // new 一个Properties 42 Properties properties = new Properties(); 43 try { 44 // 加载配置文件 45 properties.load(inputstream); 46 return properties; 47 } catch (IOException e) { 48 throw new RuntimeException(e); 49 } finally { 50 try { 51 inputstream.close(); 52 } catch (IOException e) { 53 throw new RuntimeException(e); 54 } 55 } 56 57 } 58 }