zoukankan      html  css  js  c++  java
  • java读取数据写入txt文件并将读取txt文件写入另外一个表

    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))+"
    ");//将json字符串逐行写入text 
            }
            out.flush();// 把缓存区内容压入文件  
            out.close();// 最后记得关闭文件  
            long endTime = System.currentTimeMillis();
            logger.info("===========================结束读取写入:"+endTime+"
    ===========================共耗时:"+(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+"
    ===========================共耗时:"+(endTime - startTime)/1000+"s");
        }
        
        
        //@Test
        public void testInsetBatch(){
            System.out.println(1111);
        }
    }
  • 相关阅读:
    ArcGIS 网络分析[8.1] 资料1 使用AO打开或创建网络数据集之【打开】
    【AO笔记】Addins的Toolbar 添加一条分割线
    ArcGIS 网络分析[4] 网络数据集深入浅出之连通性、网络数据集的属性及转弯要素
    ArcGIS 网络分析[2.5] VRP(车辆配送)【较难】
    ArcGIS 网络分析[2.4] OD成本矩阵
    Python中time模块详解
    python 读取文件、并以十六进制的方式写入到新文件
    【C++程序员学 python】python 之奇葩地方
    【C++程序员学 python】python 之变量
    校验值的计算----移位算法
  • 原文地址:https://www.cnblogs.com/xionggeclub/p/6963588.html
Copyright © 2011-2022 走看看