data/data/package/files/myfile.txt is not copied to apk file - java

I followed This thread
and copied "MyFile.txt" file to data/data/package/files/ through DDMS>File Explorer and hit run. The emulator displays the content of file, but, when I copy apk and run on device, it throws an exception java.io.FileNotFoundException: data/data/package/files/myfile.txt failed ENOENT.
Is it because data/data/package/files/myfile.txt is not copied to apk file?
I tried res/raw/myfile.txt it works well.
Also, if there is any way to read a file which is in the root?
Thanx.

Yes, your file wouldn't get copied to the apk this way.
The data directory is the runtime storage area for the app. It only gets created after you install your app on a device/emulator.
So, this kind of packaging your data would never work.
One way to package your data is by putting it into the assets folder. However, if you would like to edit/change the file once the application is installed, this method also would not work for your situation.
But, if you just want a read only version of this file, putting it into assets folder would work fine.

Related

Cannot parse xml file. Content is not allowed in Prolog! Java (IntelliJ)

So I am struggling to package my program into a .jar file due to the use of external libraries (Poi apache).
However, after trying to build the project into a jar file, I get this message (See title and pic).
I have tried deleting my.IDEA folder and tried to reimport, but the problem is that when I delete my.IDEA folder I also lose my external libraries which my program depends on, and therefore it becomes a cycle where I have to reinstall the java-libraries again which then creates the.IDEA folder as a result

Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ´*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader

Recovering my application project

I copied my Application Project Folder into another one to try a big change on my app.
The change was not what i wanted, so i deleted the current project folder.
I then wanted to use my TMP folder to recover my old project. I used "mv" to rename TMP into tho old name i used.
But when i came back to eclispe, i had a lot of bugs, and then all my files were deleted and are now empty...
All i have left is my app installed on a virtual device, and all my projects file with the right names but which are empty.
Is there any chance to get my code back using the apk installed on the device or something like that ?
I used this answer Is there a way to get the source code from an APK file? to decompile my app from the .pkg that was stored onto my virtual device !
I got all my drawables / assets / .xml files from this, but the code was unusable so i used what i found here ([your android projects folder]/.metadata/.plugin/org.eclipse.core.resources/.history) to bring back my java code using "grep -r" in the folder to find the pieces of code i needed !

Android Studio load file from resources

I am trying load .json files from resources so I can use them in unit tests.
I have created a resources file and marked it as a ‘resources root’ directory, and placed my .json files within it. As the files are to be used for tests only, I do not wish to place them in the /res directory as it unnecessarily bloat the app.
I am attempting to open the resources file with the following code, however inputStream returns as null.
InputStream inputStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("file.json”);
I have set the following in the .iml file that should denote the directory as resources but it seems to have had no effect.
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
Does anyone have any ideas where I maybe going wrong? Thanks in advance!
you can use assest folder or raw folder under resource folder
When the APK file for the app is built, it does not include directories that you create outside of "res" and "asset". Only files inside the APK are deployed to the device or emulator when testing. So there is no way to test a file that you do not include in the APK.
Files that are placed in the tests directory do not seem to be accessible. So AFAIK, you'll have to put the file in the app APK and then remove it later. Or, even better, it is possible to use gradle to produce a version of the app APK for testing and a different one for release. The release version could exclude the file.

Editing decompiled .jar files using Eclipse

I have a web application, precisely Cyclos SMS mobile banking software that I need to edit some of the .jar files. I imported it as a .war file into Eclipse, edited the .java file that I wanted to, run clean; when I try to check the .jar file that needed to be updated with this change with jd-gui, I see that nothing has changed in it.
So I decided to edit the .jar file by decompiling it with jd-gui, and saving it to it source file; edited the .java file I wanted to edit by importing it into Eclipse; exported it as a .war file and substituted .jar for .war.
I decided to view the changes made, but in the file I edited, I saw that it tried to call other classes in other .jar files of the whole Cyclos SMS web app but could not see them, so it only compiled with errors. How do I edit the whole Cyclos SMS web app, since it has many .jar files, and just working on one of them at a time and recompiling it alone without compiling the entire web app will always lead to errors.
To simply put the question again, how to I edit .jar files in a big web app?
Try reading the Cyclos download page http://www.cyclos.org/download, looks like sources are available. Try downloading the source code and modifying it

Categories

Resources