Files management - java

I have a ApplicationData.json file which includes some data used by my application.
The file is created before the app starts (manually) and it will never be deleted.
Sometimes, I need to override it's data by recreate the file with the updated data
.
In addition, I have more files which are generated on runtime.
I want all files to be reachable outside from my application manually.
In which path should I use while read & write to them?
Where should I locate my ApplicationData.json file before the app starts using Android Studio files explorer?

Related

Where is package folder ? that should stored in android device?

I am having problem i have just created a simple android app.I want to know that the folder of package name i.e. com.xx.xx in Android/data as usually created when we install an app is not present on my device how to solve this problem.
Is something that i am missing in manifest i have declared only permission for write external storage and everything in manifest file is same as default.
From what I have seen the folder com.xx.xx in Android/data for the normal cases which you see only gets created if you exclusively write files to the external storage using something like getExternalFilesDir() or getExternalCacheDir() method. So unless you are writing it to the external storage you might be creating them as files stored privately by the application.
If you are looking for these files stored privately by the application, i don't think these are shown by the default file browser in an unrooted device. But in an emulator or device connected to android studio, you can use "Device File Explorer".
If you don't see its tab, go to: View --> Tool Windows --> Device File Explorer.
In the Device File Explorer, Select your device then expand: data --> data --> com.xx.xx
And within it you should be able to see the privately generated files.
Also for us to understand what you are actually using, there is no way unless you add the code of how you are writing these files you mentioned.

How to read a file from inside Openwhisk Java Action

I'm trying to use openwhisk cloud functions to leverage some existing java code. The code needs to read a local file. Is this supported in a Java Action on IBM Cloud?
Though the file is included in the jar file - the application is not able to reach the file .
OpenWhisk actions can read from the file system. Changes to the runtime filesystem are not persisted across invocations.
If you want to include a file within the JAR, you will need to need to find the location of the JAR within the filesystem, unzip it and read the file contents.
The Java runtime stores the user-provided JAR in the system-wide temporary directory as useraction.jar.
Another approach would be to store the file on an object storage service and retrieve it dynamically during invocations.

where to keep an XML that should be editable and downloadable?

So I have a web app (using apache tomcat server,servlets,eclipse IDE). I wrote code to allow user to edit the XML file via UI. So I used the following code to access the XML through java
String fileName = "/MyXML.xml";
String path = this.getClass().getResource(fileName).toString();
This works fine. I am able to edit the file through UI.
Now I want to let the client download the file. But I am not able to access the file while I am trying to download.
However, if I keep the file inside webapps folder, then I can access the file using the following
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/MyXML.xml");
(Thanku Mr.MK Yong- http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/)
But then If i keep it inside webapp folder, how do I access the file for editing the XML ?
So basically I am either able to edit the file, or I am able to download the file(from webapp folder), or I am able to do both on two different copies of the file. I want to edit the XMl file and be able to download the same. So where do I keep the file and how do I access it?
You should store it in the local resource folder as it is essentially a dynamic resource.
The other thing i recommend is if you know the parameters that will be changed then have a template in resource folder and store the changes in database.
Personally i have it the second way.
e.g.
/yourapp/resource/config_file/xmltemplate.xml
Parameters that can change:
userLocation
folderLocation
colorBase
Stored them in the table:
Table: UserCongifStorage
Columns: userLocation, folderLocation, colorBase
So when i need to use the data from row 1 the logic is:
read in the xml file into a string, replace the variables with data retrieved from database, output it as xml to resource folder.
Then you read for usage.
Hope that helps
If the file is in your 'webapp' folder (I think you mean your application root), then it is already accessiable to everyone by calling hxxp://domainname/appname/MyXML.xml. I would suggest you not to store files that can be edited, inside your app folders, since they will be overwritten if you redeploy your application.
Put them in an external directory and load the contents like you would do with all other files. Doing this you can take control over file permissions easily, too.

Saving a file in an internal directory proper to an Android library

I'm working on an Android library that's meant to be used by multiple applications. The library needs to save data to a file that can be accessed by any of these applications.
Let's say I have two applications that use this library:
package com.example.app1
package com.example.app2
And that my library is under
package com.library.sdk
When the library running under app1 tries to save a file to internal storage, it is saved under com.example.app1. So if app2 wants to get access to that file, it needs to look under com.example.app1 using a ContentProvider, for example. The problem is that if app1 is uninstalled, the data saved in the file also vanishes.
Is there no way to let the library save the file to its own internal storage (i.e. under com.library.sdk)? I tried, but seem to get access violations. But shouldn't this be possible given that the library is it's own packaged project/jar?

Changing the contents of a jar from a Java program

I would like to be able to create a program that receives a .jar file (another app I've created), and modify it's contents (only add or remove files (not class files or anything), not small modifications).
If it matters, the question in hand is about an app that has a Music library, and supports adding files to it (so that if I open it up with winrar and add music files into the music directory, the app will recognize it on startup), but I would like to save the users some time, and not have them meddle with the jar, so that is why I want to create this program (so that they configure the app music library by themselves and share it with others). There are other features except music that also support this.
Use TrueVFS (the successor of TrueZIP) to access and modify the content of your JAR file as a virtual file system. With this you can change the JAR file in place without the need to extract and repackage it.

Categories

Resources