简体   繁体   English

从CSV文件读取

[英]Reading from a CSV file

try {
    BufferedReader br = new BufferedReader(new InputStreamReader(item.getInputStream()));
    String strLine = "";
    StringTokenizer st = null;
    while ((strLine = br.readLine()) != null) {
        st = new StringTokenizer(strLine, "\t");
        while (st.hasMoreTokens()) {
            urlcnt = st.nextToken();
            srccnt = st.nextToken();
            contentType = st.nextToken();
            verticle = st.nextToken();
            timeFrame = st.nextToken();
        }
        if (con == null) {
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con = SQLConnection.getNewConnection();
            stmt = con.createStatement();
        }
        try {
            ResultSet rs;
            boolean hasRows = false;
            rs = stmt.executeQuery("select url from urls_temp where url='"+urlcnt+"'");
            while (rs.next()) {
                hasRows=true;
                i++;
            }
            if (!hasRows) {
                j++;
                PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
                if (timeFrame.equalsIgnoreCase("Daily")) {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 0);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                    insertUrlStatement.close();
                } else {
                    insertUrlStatement.setString(1, urlcnt);
                    insertUrlStatement.setString(2, srccnt);
                    insertUrlStatement.setInt(3, 1);
                    insertUrlStatement.setInt(4, 1);
                    insertUrlStatement.setString(5, contentType);
                    insertUrlStatement.setInt(6, 1);
                    insertUrlStatement.setString(7, verticle);
                    insertUrlStatement.setString(8, timeFrame);
                    insertUrlStatement.executeUpdate();
                }
            }
        }
    }
}

The above code is used for uploading details to the database that are given tab separated in a CSV file. 上面的代码用于将详细信息上载到数据库,这些详细信息在CSV文件中以制表符分隔。

Sample format of CSV file will be as follows and this works fine: CSV文件的示例格式如下,可以正常工作:

http://avb.com(tab space)asdf(tab space)asdf(tab space)asdd(tab space)asdf

http://anything.com(tab space)asdf(tab space)asdf(tab space)asdfasd(tab space)asdfsadf

Sometimes I may need some null values to be inserted into the database from the CSV file as follows: 有时我可能需要一些null值从CSV文件插入数据库中,如下所示:

http://asdf.com(tab space)(tab space)aasddf(tab space)(tab space)asdfsad

But this is not working, nothing is getting inserted into the database. 但这是行不通的,没有任何内容插入数据库。

What modification has to be done to the above program for inserting null values in the second and fourth ( srccnt & verticle ) columns of the table? 什么修改已做上述程序用于插入null在第二和第四(值srccntverticle表中的)列?

StringTokenizer treats consecutive delimiters as a single delimiter. StringTokenizer将连续的定界符视为单个定界符。 You say the input contains [tab space] as delimiters, but the rest of your code doesn't seem to be expecting spaces so, without more information, I'm going to guess that the input is delimited only with tabs (not [tab space]), and the adjacent delimiters are being ignored, Then the last nextToken() throws an exception which you are ignoring. 您说输入包含[制表符空格]作为定界符,但是您的其余代码似乎没有期望空格,因此,如果没有更多信息,我将猜测输入仅用制表符分隔(而不是[制表符空格]),并忽略相邻的定界符,然后最后一个nextToken()会引发一个您正在忽略的异常。

The answer here is to rewrite this using split() , as recommended in the Javadoc 答案是按照Javadoc的建议使用split()重写它

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. StringTokenizer是一个遗留类,出于兼容性原因而保留,尽管在新代码中不鼓励使用它。 It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. 建议任何寻求此功能的人改用String的split方法或java.util.regex包。

That said, you should look at any of the existing CSV libraries out there (Google for Java CSV). 也就是说,您应该查看其中的任何现有CSV库(Google for Java CSV)。

我建议您调试代码,以更具体地找出不符合您期望的行为,然后在必要时发布更具体的问题。

I use FileHelpers for parsing csv files. 我使用FileHelpers解析csv文件。 http://www.filehelpers.com/ . http://www.filehelpers.com/ It reads a csv file and gives me an output in object, which is what I desire. 它读取一个csv文件,并在对象中给我输出,这是我想要的。 And also if I pass the list of objects it would create a csv file for me. 而且,如果我传递对象列表,它将为我创建一个csv文件。 Give it a chance. 给它一个机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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