package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.List; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.fastjson.JSON; import com.xsw.model.BatchTemp; import com.xsw.model.TeamsDTO; import com.xsw.service.BatchService; import com.xsw.service.TeamService; import com.xsw.util.DateUtils; @RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类 @ContextConfiguration(locations = { "classpath:spring.xml" ,"classpath:spring-mybatis.xml"}) public class MybatisTest { private static Logger logger = Logger.getLogger(MybatisTest.class); @Resource private TeamService teamService; @Resource private BatchService batchService; @Test //@Ignore public void testTeam() throws IOException{ TeamsDTO cre = teamService.getTeamById(1L); long startTime = System.currentTimeMillis(); logger.info("===========================开始执行读取写入:"+startTime); File file = new File("F:\\"+DateUtils.date2SStr()+".txt"); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); List<TeamsDTO> list = teamService.selectAllList(); for (int i = 0; i < list.size(); i++) { //logger.info(i+"|"+JSON.toJSON(list.get(i))); out.write(JSON.toJSONString(list.get(i))+"\r\n");//将json字符串逐行写入text } out.flush();// 把缓存区内容压入文件 out.close();// 最后记得关闭文件 long endTime = System.currentTimeMillis(); logger.info("===========================结束读取写入:"+endTime+"\r\n===========================共耗时:"+(endTime - startTime)/1000+"s"); //logger.info(JSON.toJSON(cre)); startTime = System.currentTimeMillis(); logger.info("===========================开始读取:"+startTime); /* 读入TXT文件 */ InputStreamReader reader = new InputStreamReader( new FileInputStream(file),"UTF-8"); // 建立一个输入流对象reader BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = ""; line = br.readLine(); while (line != null) { line = br.readLine(); // 一次读入一行数据 //System.out.println(line); batchService.insertSelective(JSON.parseObject(line, BatchTemp.class)); } endTime = System.currentTimeMillis(); logger.info("===========================读取完成:"+endTime+"\r\n===========================共耗时:"+(endTime - startTime)/1000+"s"); } //@Test public void testInsetBatch(){ System.out.println(1111); } }