由网友(吻到嘴边的泪°狠咸)分享简介:你好我试图读取资产脱颖而出,并希望将其转换成JSON,但我得到的错误:打开失败:ENOENT(没有这样的文件或目录)后,搜查了许多SO问题但无法找到解决方案下面是我的code 公共无效readXlsFileAndConvertToJsonObject(){ JSONArray jsonArray =新JSON...
你好我试图读取资产脱颖而出,并希望将其转换成JSON,但我得到的错误:打开失败:ENOENT(没有这样的文件或目录)后,搜查了许多SO问题但无法找到解决方案下面是我的code
公共无效readXlsFileAndConvertToJsonObject(){ JSONArray jsonArray =新JSONArray(); JSONArray mainJsonArray =新JSONArray(); 尝试{ 档案文件=新的文件(文件:///android_asset/filters.xls); FIS的FileInputStream =新的FileInputStream(文件); //最后的FileInputStream文件=新的FileInputStream(新文件(filters.xls)); 诠释计数= 0; //获取XLS文件的工作簿实例 HSSFWorkbook工作簿=新HSSFWorkbook(FIS); //从工作簿中第一张 HSSFSheet片= workbook.getSheetAt(0); //通过从第一张各行迭代 迭代器<行> rowIterator = sheet.iterator(); 而(rowIterator.hasNext()){ 鳞次栉比= rowIterator.next(); //LOGGER.info("count是+计数); 如果(计数== 0 ||计数== 1){}其他{ 尝试{ JSONObject的jsonMaterialObject =新的JSONObject(); // 名称 jsonMaterialObject.put(姓名,row.getCell(0) 的ToString()); //厚内斯 jsonMaterialObject.put(厚度,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,排 .getCell(7)的ToString()); jsonMaterialObject.put(medium_frequency_rank,排 .getCell(8)的ToString()); jsonMaterialObject.put(high_frequency_rank,排 .getCell(9)的ToString()); //添加文件大小 最终文件DIR =新的文件(文件:/// android_asset /文件名); 最终文件[] = ListFiles dir.listFiles(); 布尔发现= FALSE; 如果(ListFiles.length == 0){}其他{ 对于(文件å文件:ListFiles){ 的System.out.println(å文件名称+ aFile.getName()); 的System.out.println(从JSON文件名的文件+ row.getCell(0)的ToString()); 如果(aFile.getName()。equalsIgnoreCase( row.getCell(0)的ToString()+.PDF)){ jsonMaterialObject.put(档案大小,truncateDecimal(aFile.length()/ 1024,2)); 发现= TRUE; } 如果(找到){ 打破; } } jsonMaterialObject.put(pdfFileName,排 .getCell(0)的ToString()+.PDF); } jsonArray.put(jsonMaterialObject); }赶上(JSONException E){ // TODO自动生成catch块 e.printStackTrace(); } } 算上++; } ((可关闭)文件).close(); }赶上(FileNotFoundException异常五){}赶上(IOException异常五){} createMaterialJson(jsonArray.toString(),finalfile.json); }
我提到的权限如下
<使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAGE/> <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/> <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAGE/>
下面是我的跟踪
12月11日至25日:32:35.535:我/的System.out(20019):异常的readXlsFileAndConvertToJsonObject /file:/android_asset/filters.xls计提坏账:打开失败:ENOENT(无这样的文件或目录)
解决方案
此问题浪费了我个小时的时间:P
彻底记录检查的文件名。
此问题是配备了一个带数字的一些文件名。
我通过改变文件名解决了这个。
Hi i am trying to read excel from assets and wanted to convert it into JSON, But i am getting the error: open failed:ENOENT(No such file or directory), searched many SO questions but could not find the solution Below is my code
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");
}
I mentioned the permissions as below
<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"/>
Below is my Trace
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)
解决方案
This issue wasted my hours of time :P
Thoroughly check your file names by Logging.
This issue is comes with some file names with digits.
I solved this by changing the file name.
相关推荐
最新文章