Android: open failed: ENOENT (No such file or directory)
Asked Answered
A

2

7

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)
Approbate answered 25/11, 2014 at 7:13 Comment(0)
T
7

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.

Thermotherapy answered 25/11, 2014 at 7:18 Comment(2)
How did you change file name? Please see this question: #30883333. Please help me to solve my problem. Thanks in advance.Sukkoth
what is wrong with filenames with numbers, is there any way out without having to change file names?Endear
S
0

Use getAssets().open("filter.xls") (It returns an InputStream). Call this on a context or activity. Only this worked for me in some cases.

Sadoc answered 25/11, 2014 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.