我正在尝试从Google驱动器加载 CSV 文件并在java中解析它。但每当我选择该文件时,它会显示异常:找不到文件。
以下是我实施的代码:
选择器意图代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode)
{
case 100:
{
if (resultCode == RESULT_OK)
{
//onImport(new File(data.getData().getPath()));
String filePath = null;
Uri uri = data.getData();
File file = new File(uri.toString());
if (file.exists()) {
//Do something
System.out.println("Exists");
proImportCSV(new File(data.getData().getPath()));
}
else
{
System.out.println("Not Exists");
}
}
}
}
}
onActivity结果:
private void proImportCSV(File from)
{
try
{
// reading CSV and writing table
CSVReader reader = null;
try
{
reader = new CSVReader(new FileReader(from));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] nextLine;
try {
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
使用 CSVReader
解析CSV{{1}}
请告诉我为什么我找不到文件错误。另外,我正确地解析了csv吗?如果不对,请告诉我正确的详细信息。
提前致谢!
答案 0 :(得分:1)
我使用bufferReader解析它,一切都很完美!
Uri uri = data.getData();
BufferedReader mBufferedReader = null;
String line;
try
{
InputStream inputStream = getContentResolver().openInputStream(uri);
mBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = mBufferedReader.readLine()) != null)
{
System.out.println("LLLLL: "+ line);
}
mBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}