0

I have a program that I want to send some data to an excel sheet using the jxl library. I'm following a tutorial and have the following code snippet that I believe should create an .xls file with a Sheet and some data. However I'm getting a file not found error with whatever path I give. If I enter no path there is a write-only system error although I have read/write internal/external permissions in my manifest. If someone could point out the error I would appreciate it as I'm still somewhat new to java/android.

         try {
                String exlFile = "Workbook1.xls";
                WritableWorkbook writableWorkbook = Workbook.createWorkbook(new File("/Users/xxxxxx/Documents/", exlFile));

                WritableSheet writableSheet = writableWorkbook.createSheet("Sheet1test", 0);

                //Create Cells with contents of different data types.
                //Also specify the Cell coordinates in the constructor
                Label label = new Label(0, 0, "Label (String)");
                DateTime date = new DateTime(1, 0, new Date());


                //Add the created Cells to the sheet
                writableSheet.addCell(label);
                writableSheet.addCell(date);


                //Write and close the workbook
                writableWorkbook.write();
                writableWorkbook.close();

            } catch (IOException e) {
                e.printStackTrace();
            } catch (RowsExceededException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }

Error:

java.io.FileNotFoundException: /Users/xxxxxx/Documents/Workbook1.xls (No such file or directory)
lexalenka
  • 307
  • 4
  • 16
  • `(new File("/Users/xxxxxx/Documents/", exlFile));`. That path does not exist on an Android device. Hence the exception. It looks more -part of- a windows path. – greenapps Nov 01 '16 at 19:23
  • i thought i should be able to save it to my documents or some external storage device – lexalenka Nov 01 '16 at 20:04

2 Answers2

0

java.io.FileNotFoundException: the system cannot find the file specified

For example, if you changed your project structure to ProjectRoot\src\resources\word.txt, you could use this:

InputStream is = Hangman1.class.getResourceAsStream("/resources/word.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is));

Community
  • 1
  • 1
0

Change

(new File("/Users/xxxxxx/Documents/", exlFile));

To

 (new File(Environment.getExternalStorageDirectory(), exlFile));
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • java.io.FileNotFoundException: /storage/emulated/0/Workbook1.xls (Permission denied) .....but I have in manifest: – lexalenka Nov 01 '16 at 20:03
  • On Android 6 and above you need runtime permission too. Or go to settings of your app and toggle the storage switch for it. – greenapps Nov 01 '16 at 20:35
  • Toggling the storage switch on my emulator stopped the permission denied, but I'm unable to find the xls file :( – lexalenka Nov 01 '16 at 21:18
  • Where and how did you look? You should check if a file exists with `File.exists()`. – greenapps Nov 01 '16 at 21:19
  • just through searching the emulator - exists returns true – lexalenka Nov 01 '16 at 21:56
  • I do not know what you are doing when you say 'search through the emulator'. How could anybody know? So the file exists. Then your problem is so!ved. – greenapps Nov 01 '16 at 22:16
  • On a real device I receive open failed: EACCES (Permission denied) - which I believe to be a runtime permission issue as mentioned before – lexalenka Nov 01 '16 at 22:52
  • http://stackoverflow.com/questions/23527767/open-failed-eacces-permission-denied to request permissions fixed the issue now it works – lexalenka Nov 01 '16 at 22:58