zoukankan      html  css  js  c++  java
  • File操作-将txt里的内容写入到数据库表

    package com.Cristin.File;//将txt里的内容写入到数据库表

    import com.Cristin.MySQL.AddDataToDB;
    import org.testng.annotations.Test;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;

    /**
    * Created by cristin on 2017/8/2.
    */
    public class FileReader {

    /**
    * 读取指定路径的Txt文件,插入到数据库
    * @param filePath
    */
    @Test
    public static void readTxtFile(String filePath){
    try {
    String encoding = "UTF-8";
    File file = new File(filePath);
    if (file.isFile() && file.exists()){//判断文件是否存在
    FileInputStream in = new FileInputStream(file);
    InputStreamReader read = new InputStreamReader(in,encoding);//考虑到编码格式
    BufferedReader bufferedReader = new BufferedReader(read);
    String lineTxt = null;
    while ((lineTxt = bufferedReader.readLine()) != null){
    String[] p = lineTxt.split(",");
    AddDataToDB.insert(p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
    }
    read.close();
    }else {
    System.out.print("找不到指定的文件");
    }
    }catch (Exception e){
    System.out.print("读取文件内容错误");
    e.printStackTrace();
    }
    }

    /**
    *
    * @param args
    */
    public static void main(String[] args) {
    //写入的文件路径
    String path = "/Users/cristin/Documents/CristinTestDevelopment/Resource/SQLWrite.txt";
    readTxtFile(path);
    }
    }
  • 相关阅读:
    .Net Core Swagger配置
    MySQL如何使用索引
    一个HTTP Basic Authentication引发的异常
    跑步花钱吗?
    跑步花钱吗?
    OpenShift中的持续交付
    在AWS中部署OpenShift平台
    壮美大山包-2017中国大山包国际超百公里ITRA积分赛赛记
    膝盖中了一箭之康复篇-两周年纪念
    HashiCorp Vault介绍
  • 原文地址:https://www.cnblogs.com/cristin/p/7645517.html
Copyright © 2011-2022 走看看