我正在编写一个Java库来从CSV文件导入数据,并根据一些正则表达式测试的结果将结果返回为INTEGER,DECIMAL,VARCHAR和其他列类型。
因此,我需要仔细阅读ResultSet
返回的Csv.read()
两次;一次确定列类型并再次填充新SimpleResultSet
(或者最好将原始ResultSet
包装在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]
我会很感激为什么这不起作用的任何建议,或者其他方式的建议!
更新
因为H2 Csv.read()
函数产生了一个无法倒带的ResultSet
,所以我最终复制了第一遍中读取的源InputStream
,然后重放了结果byte[]
1}}在第二遍。这样,我可以确保导入相同的数据。这是我写的帮助类:
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);
答案 0 :(得分:2)
您无需阅读ResultSet
两次。 您可以使用非标准H2命令 CSVREAD
填充文件中的表格。
或者,你可以再次创建Csv对象。