我正在尝试访问我放入 Android 项目的 res/raw 文件夹中的文件。我正在使用这个 .CSV 文件与CsvJdbc jar 一起使用,它允许我像查询数据库一样查询它。我见过很多用 InputStream 抓取它的例子,例如......
InputStream raw;
raw = getResources().openRawResource(R.raw.vehicles);
但我需要通过创建连接来获取文件。IE
Connection conn = DriverManager.getConnection("jdbc:relique:csv:PathToFolderWhereCsvFileIsGoesHere");
这是完整的代码。
String[] holder = null;
try
{
// load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver");
// create a connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = DriverManager.getConnection("jdbc:relique:csv:PathToFolderWhereCsvFileIsGoesHere");
// create a Statement object to execute the query with
Statement stmt = conn.createStatement();
// Select the ID and NAME columns from sample.csv
ResultSet results = stmt.executeQuery("SELECT model FROM vehicles where id = 200");
// dump out the results
for(int i=0; i < results.getFetchSize(); i++){
holder[i] = results.getString("model");
}
// clean up
results.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}