1

我正在编写一个 Java 库来从 CSV 文件中导入数据,并根据一些正则表达式测试的结果将结果返回为 INTEGER、DECIMAL、VARCHAR 和其他列类型。

因此,我需要通读ResultSet返回的Csv.read()两次;一次确定列类型,然后再次填充一个新的SimpleResultSet(或者最好将原始的包装ResultSet在 a 中SimpleRowSource,这样我就不必存储表数据)。我试过打电话ResultSet.beforeFirst(),但第二遍没有可用的行。

// Create an input stream with test data.
String data =
    "name,value\n" +
    "one,1\n" +
    "two,2\n" +
    "three,3\n";
InputStream stream = new ByteArrayInputStream(data.getBytes());
Reader reader = new InputStreamReader(stream);

// Read the test data into a new ResultSet.
Csv csv = new Csv();
ResultSet resultSet = csv.read(reader, null);

// Iterate over the ResultSet on first pass.
int firstPass = 0;
while (resultSet.next())
{
    // Test all values for integer, decimal etc.
    ++firstPass;
}
System.out.println("Read " + firstPass + " row(s) on first pass");

// Move the cursor to before the first row; this doesn't work!
resultSet.beforeFirst();

// Check if the cursor is before the first row; throws exception!
/* resultSet.isBeforeFirst(); */

// Iterate over the ResultSet on second pass.
int secondPass = 0;
while (resultSet.next())
{
    // Add all values to new SimpleResultSet, or wrap this code
    // in an H2 SimpleRowSource, and pass to the constructor of
    // a new SimpleResultSet.
    ++secondPass;
}
System.out.println("Read " + secondPass + " row(s) on second pass");

这会产生以下输出:

Read 3 row(s) on first pass
Read 0 row(s) on second pass

我注意到这beforeFirst()不会引发异常,但isBeforeFirst()会:

org.h2.jdbc.JdbcSQLException: Feature not supported: null [50100-172]

我将不胜感激有关为什么这不起作用或其他方式的任何建议!

更新

因为 H2Csv.read()函数产生的 aResultSet不能倒带,所以我最终复制了源代码InputStream,因为它在第一遍中读取,然后byte[]在第二遍中重放结果。这样,我可以确定导入相同的数据。这是我为此编写的助手类:

public class CopyStream extends InputStream
{
    private final InputStream mInputStream;
    private final ByteArrayOutputStream mOutputStream;

    public CopyStream(InputStream inputStream)
    {
        // Initialise the class.
        mInputStream = inputStream;
        mOutputStream = new ByteArrayOutputStream();
    }

    public InputStream copy()
    {
        // Return a new input stream based on the copied data.
        byte[] buffer = mOutputStream.toByteArray();
        return new ByteArrayInputStream(buffer);
    }

    @Override
    public int read() throws IOException
    {
        // Read a byte from the input stream.
        int b = mInputStream.read();
        if (b >= 0)
        {
            // Copy the byte to the output stream.
            mOutputStream.write(b);
        }
        return b;
    }

    @Override
    public int read(byte[] buffer) throws IOException
    {
        // Read some bytes from the input stream.
        int length = mInputStream.read(buffer);
        if (length > 0)
        {
            // Copy the bytes to the output stream.
            mOutputStream.write(buffer, 0, length);
        }
        return length;
    }

    @Override
    public int read(byte[] buffer, int offset, int length) throws IOException
    {
        // Read some bytes from the input stream.
        length = mInputStream.read(buffer, offset, length);
        if (length > 0)
        {
            // Copy the bytes to the output stream.
            mOutputStream.write(buffer, offset, length);
        }
        return length;
    }
}

用法如下:

Csv csv = new Csv();
CopyStream copyStream = new CopyStream(inputStream);
ResultSet resultSet1 = csv.read(new InputStreamReader(copyStream), null);
ResultSet resultSet2 = csv.read(new InputStreamReader(copyStream.copy()), null);
4

1 回答 1

2

你不需要读ResultSet两遍。您可以使用非标准 H2 命令CSVREAD从文件中填充表。

或者,您可以再次创建 Csv 对象。

于 2013-11-10T14:19:43.013 回答