gpt4 book ai didi

Android:打开失败:ENOENT(没有那个文件或目录)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:07 25 4
gpt4 key购买 nike

您好,我正在尝试从 Assets 中读取 excel 并想将其转换为 JSON,但我遇到了错误:打开失败:ENOENT(没有这样的文件或目录),搜索了许多 SO 问题但找不到解决办法下面是我的代码

public void readXlsFileAndConvertToJsonObject() {
JSONArray jsonArray = new JSONArray();
JSONArray mainJsonArray = new JSONArray();
try {

File file = new File("file:///android_asset/filters.xls");
FileInputStream fis = new FileInputStream(file);
//final FileInputStream file = new FileInputStream(new File("filters.xls"));

int count = 0;
// Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(fis);

// Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

// Iterate through each rows from first sheet
Iterator < Row > rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//LOGGER.info("count is " + count);
if (count == 0 || count == 1) {} else {
try {
JSONObject jsonMaterialObject = new JSONObject();
// Name
jsonMaterialObject.put("name", row.getCell(0)
.toString());
// thick ness
jsonMaterialObject.put("thickness", row.getCell(4)
.toString());
// rating_1
jsonMaterialObject.put("rating_1", row.getCell(5)
.toString());
// rating_2
jsonMaterialObject.put("rating_2", row.getCell(6)
.toString());

jsonMaterialObject.put("low_frequency_rank", row
.getCell(7).toString());

jsonMaterialObject.put("medium_frequency_rank", row
.getCell(8).toString());

jsonMaterialObject.put("high_frequency_rank", row
.getCell(9).toString());

// Add file size
final File dir = new File("file:///android_asset/filename");
final File[] ListFiles = dir.listFiles();
boolean found = false;
if (ListFiles.length == 0) {} else {
for (File aFile: ListFiles) {
System.out.println("Afile Name " + aFile.getName());
System.out.println("A file from json Name " + row.getCell(0).toString());
if (aFile.getName().equalsIgnoreCase(
row.getCell(0).toString() + ".pdf")) {
jsonMaterialObject.put("fileSize", truncateDecimal(aFile.length() / 1024, 2));
found = true;
}
if (found) {
break;
}
}

jsonMaterialObject.put("pdfFileName", row
.getCell(0).toString() + ".pdf");
}

jsonArray.put(jsonMaterialObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count++;

}
((Closeable) file).close();

} catch (FileNotFoundException e) {} catch (IOException e) {}
createMaterialJson(jsonArray.toString(), "finalfile.json");
}

我在下面提到了权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

下面是我的轨迹

11-25 12:32:35.535: I/System.out(20019): Exception accured in readXlsFileAndConvertToJsonObject /file:/android_asset/filters.xls: open failed: ENOENT (No such file or directory)

最佳答案

这个问题浪费了我几个小时的时间:P

通过日志记录彻底检查您的文件名。

此问题与一些带数字的文件名有关。

我通过更改文件名解决了这个问题。

关于Android:打开失败:ENOENT(没有那个文件或目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27120720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com