いくつかの正規表現テストの結果に従って、CSV ファイルからデータをインポートし、結果を INTEGER、DECIMAL、VARCHAR、およびその他の列タイプとして返す Java ライブラリを作成しています。
したがって、ResultSet
返された by をCsv.read()
2 回読む必要があります。1 回は列の型を決定し、もう一度新しいデータを入力します(または、できれば元のデータをでSimpleResultSet
ラップするため、テーブル データを保存する必要はありません)。を呼び出してみましたが、2 回目のパスで使用できる行がありません。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]
これが機能しない理由、またはこれを行う別の方法についての提案をいただければ幸いです。
アップデート
H2Csv.read()
関数はResultSet
巻き戻しできない を生成するためInputStream
、最初のパスで読み取られたソースをコピーし、2 番目のパスで結果を再生することになり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);