简体   繁体   中英

how deal with spaces/tabs in txt files

i am making a program where i would load data from txt files in mysql tables.I would create tables with specific fields and then I would load the data from the txt files in them. I am using java for doing the program.

What I have written is the below:

private static String importData(Connection con, File txtFile,
    String tablename) {

  try {
    Statement stmt;

    stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    String path = txtFile.getAbsolutePath();
    String importingdata = "LOAD DATA INFILE '"
            + path.replace('\\', '/')
            + "' INTO TABLE " + tablename
            + " FIELDS TERMINATED BY '\t'";
    System.out.println("fill the table");
    stmt.executeUpdate(importingdata);

    } catch (Exception e) {
    System.out.println(((SQLException) e).getSQLState());
    System.out.println(e.getMessage());
    e.printStackTrace();

     }
   return null;
    }

But in this code as you can see I has said that the fields are terminated by tab. What if the columns in the text files are separated by both spaces and tabs with unknown number of them?

For example the text that shown in the image:

在此输入图像描述

that the first column is separated from the second by one tab and 3 spaces and the second column from the third column by 2 spaces. All my text files have specific fields but the separated spaces between them is unknown. Is there a way to read all these space until find the next column that contain data?

There are many ways to do this. One is to read the file a line at a time, and then split the lines into fields using String.split(regex) ; eg

    String line = ...
    String fields[] = line.split("\\s+");

Then rewrite the SQL so that you can insert one row at a time.

Or better still, use batches; eg as described here: http://viralpatel.net/blogs/batch-insert-in-java-jdbc/

我认为应该指定你的文本文件如何包含空格或带有tab的数据,简而言之应该是某种统一性,因为sql loader terminated by只使用一个参数来标识列。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM