java 从数据库取值_java操作txt文件,取值-转实体-并保存到数据库
发布日期:2021-10-27 12:03:54 浏览次数:4 分类:技术文章

本文共 2762 字,大约阅读时间需要 9 分钟。

java操作txt文件,取值-转实体-并保存到数据库

txt文件为 filetxt.txt,内容为

标题:测试txt文件

编号价格日期  名称

00011.01 20170323010101 苹果

000210.01 20170323010101 桃子

位于   WEB-INF/file/filetxt.txt,路径要注意使用了枚举,应用的话直接改为字符串

package com.bestcxx.mavenstu.mavenssh.file;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.math.BigDecimal;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import com.bestcxx.mavenstu.mavenssh.model.FileTxtModel;

import com.bestcxx.mavenstu.mavenssh.service.FileTxtModelService;

import com.bestcxx.mavenstu.mavenssh.util.DateUtil;

import com.bestcxx.mavenstu.mavenssh.util.EnumUtil;

/**

*

* @theme 对txt文件进行按行、列操作

* @author wujie

* @Datetime 2017年3月23日 上午10:26:12

*/

public class FileTxt {

private static Logger logger = LogManager.getLogger(FileTxt.class);

@Autowired

private FileTxtModelService fileTxtModelService;

public FileTxt(){

}

public FileTxt(FileTxtModelService fileTxtModelService){

this.fileTxtModelService=fileTxtModelService;

}

String filePath = EnumUtil.FILE_TXT_PATH.toString();// 文本路径 src/main/webapp/WEB-INF/file/filetxt.txt

String encoding = EnumUtil.COMMON_ENCODING.toString();// 编码格式

public void ReadFileTxt() {

File txtFile = new File(filePath);// 读取文件

InputStream in = null;

InputStreamReader read = null;

BufferedReader reader = null;

try {

in = new FileInputStream(txtFile);

read = new InputStreamReader(in, encoding);// 构建文件缓存

reader = new BufferedReader(read);

String temLine = "";// 每一行的内容

// 默认输出前两行

logger.info("第一行:" + reader.readLine());

logger.info("第二行:" + reader.readLine());

// 循环遍历txt文件

while ((temLine = reader.readLine()) != null) {

String[] split = temLine.split("\\s");// 以多个空格作为分隔符

for (String tem : split) {

logger.info(tem);

}

FileTxtModel fileTxtModel = new FileTxtModel();

fileTxtModel.setIdentifier(split[0]);

fileTxtModel.setPrice(new BigDecimal(split[1]));

fileTxtModel.setDateTime(DateUtil.getDateFromStr_yyyyMMddHHmmss(split[2]));

fileTxtModel.setName(split[3]);

fileTxtModelService.add(fileTxtModel);

}

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

logger.error(e.toString());

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

logger.error(e.toString());

} catch (IOException e) {

// TODO Auto-generated catch block

logger.error(e.toString());

} finally {

try {

if (in != null) {

in.close();

}

if (read != null) {

read.close();

}

if (reader != null) {

reader.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

转载地址:https://blog.csdn.net/weixin_34620658/article/details/114216800 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java axis 拒绝连接_java-AXIS2如何设置连接重试?
下一篇:checksignature java_微信公众平台 checkSignature(官方验证消息真实性方法) 导致回复的消息丢失...

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月26日 09时21分16秒