3
votes

In my code I am prompting the user to load a json file.

I am then attempting to copy this file into an sqlite database.

Once I have the data I am then able to manipulate it as needed - but I need to get it there in the first place.

So step 1 is to get the data in.

I have progressed as far as prompting the user to navigate to the file they want - but when I try and read the file I get this error ..

ERROR: resources must reside in the root directory thus must start with a '/' character in Codename One! Invalid resource: file:///tmp/temp3257201851214246357..json

So I think that I need to copy this file to the root directory

I cannot find a link that shows me how to do this.

Here is my code so far ...

                    case "Import Script":
                    try
                    {
                        JSONParser json = new JSONParser();

                        if (FileChooser.isAvailable()) {
                            FileChooser.showOpenDialog(".json", e2-> {
                                String file = (String)e2.getSource();
                                if (file == null) {
                                    home.add("No file was selected");
                                    home.revalidate();
                                } else {
                                    home.add("Please wait - busy importing");
                                    home.revalidate();
                                    String extension = null;
                                    if (file.lastIndexOf(".") > 0) {
                                        extension = file.substring(file.lastIndexOf(".")+1);
                                    }
                                    if ("json".equals(extension)) {
                                        FileSystemStorage fs = FileSystemStorage.getInstance();
                                        try {
                                            InputStream fis = fs.openInputStream(file);                                               

                                            try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), file), "UTF-8"))
                                            {

                                                Map<String, Object> data = json.parseJSON(r);
                                                Result result = Result.fromContent(data);

...... I progress from here

The error is occurring on this line ...

try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), file), "UTF-8"))

If I hard code a filename and manually place it in the /src folder it works ... like this ...

try(Reader r = new InputStreamReader(Display.getInstance().getResourceAsStream(getClass(), '/test.json'), "UTF-8"))

But that defeats the purpose of them selecting a file

Any help would be appreciated

Thanks

1
I have found some code here that copies a file to the application path - but it deals with images ... codenameone.com/javadoc/com/codename1/io/FileSystemStorage.html .... I am not sure how to replace the image specific code with json text code ????Steven Mark Integration

1 Answers

1
votes

I suggest watching this video.

It explains the different ways data is stored. One of the core sources of confusion is the 3 different ways to store files:

  • Resources
  • File System
  • Storage

getResourceAsStream returns a read only path that's physically embedded in the jar. It's flat so all paths to getResourceAsStream must start with / and must have only one of those. I would suggest avoiding more than one . as well although this should work in theory.

The sqlite database must be stored in file system which is encapsulated as FileSystemStorage and that's really the OS native file system. But you can't store it anywhere you want you need to give the DB name to the system and it notifies you where the file is stored and that's whats explained in the code above.