java 将一个文件写入另一个文件_java读取数据写入txt文件并将读取txt文件写入另外一个表...

packagecom.xsw.test;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.util.List;importjavax.annotation.Resource;importorg.apache.log4j.Logger;importorg.junit.Ignore;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.alibaba.fastjson.JSON;importcom.xsw.model.BatchTemp;importcom.xsw.model.TeamsDTO;importcom.xsw.service.BatchService;importcom.xsw.service.TeamService;importcom.xsw.util.DateUtils;

@RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类

@ContextConfiguration(locations = { "classpath:spring.xml" ,"classpath:spring-mybatis.xml"})public classMybatisTest {private static Logger logger = Logger.getLogger(MybatisTest.class);

@ResourceprivateTeamService teamService;

@ResourceprivateBatchService batchService;

@Test//@Ignore

public void testTeam() throwsIOException{

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 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 voidtestInsetBatch(){

System.out.println(1111);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值