I need to open a file in libgdx for random access, i.e. I need to be able to seek() to different parts of the file (not read sequentially).
Using libgdx I am able to access the file via Gdx.files.internal(), but libgdx's filehandlers don't support random access methods like seek(). I tried using java.io.RandomAccessFile, but it generates the exception No such file or directory, probably because the file is stored internally in the jar file.
How can I access the file using java.io.RandomAccessFile` or alternatively how can i open file for random-access in libgdx?
This needs to work on both Android and desktop platforms.
This is not a Libgdx limitation. You cannot do random access on files stored inside a JAR file (since they're compressed, you need to stream the contents). (I can't find a concise reference for this, but look at the definitions of JarFile and ZipFile: they only let you create streaming file handles).
Libgdx itself runs into this problem. It stores native libraries in a .jar file (the libgdx-natives.jar). To use the files, it extracts them to the local filesystem and uses them from there. See SharedLibraryLoader.java.
As far as I can tell there are three workarounds to chose from:
Remove the need for the random access in your code.
Stream the file from the JAR into memory, and randomly access it there
Copy the file from the JAR into local (private) storage or temp storage (hopefully this could be done once and not re-done on each run of the app).
Related
I've got an xml file I need to access(read only) from core. I tried putting it in the Android assets folder since that's shared, but ironically only the desktop version of my app can access it, the Android one doesn't see it at all. "Gdx.files.getLocalStoragePath" points to a different location on the Android version. Is there a spot I can store an xml file so I can access it via core?
create FileHandle object using your file.
FileHandle handle = Gdx.files.internal("data/myfile.xml");
parse that using XmlReader, that is in libgdx bundle.
I have a swing application that uses many data files, these data files will change time to time. How can I load these data files on client's machine? Is there any way to create a folder like structure and run a batch file or so? Any help is appreciated.
There are several ways to do this:
Assume you want to ship your application with the datafiles, you may embed them as a zip/jar in your application-jar-file.
Extract the embedded zip to a temporary local file and use ZipFileSystemProvider to extract the content to some place on the disc.
Here is an example how to extract some content from zip/jar-file embedded in a .jar-file downloaded by JWS.
Same as 1, but skip the zip stuff and instead provide a list of all the resources you want to extract
One other way is to create the files pragmatically using either java.nio.file (java 7+) or java.io.File
I'm working on a java project that requires me to access a file within multiple embedded zip files and directories.
For example, archive1.zip/archive1/archive2.zip/archive2/directory1/file_that_I_need.txt.
It would be a lot easier if when each zip file was extracted, it would immediately list its contents but instead there's a folder inside that contains all the contents.
The examples I found online deal with zip files that, when extracted, contain the files they need to access but I can't find any that deal with accessing files within a directory in a zip file. Any advice on this would be great.
Thanks!
Given the prohibition against creating new files, you're pretty much stuck with ZipInputStream. When you find the ZipEntry that corresponds to the embedded archive, you then read its stream to find the actual file. You can proceed recursively through as many levels of archives as you want.
This works OK if you're looking to process a single file. However, re-reading the archives for multiple files can be expensive. A better solution is to at least open the outer archive as a ZipFile, which memory-maps the actual file.
If you can then extract the contained archives into a temporary directory and open them as ZipFiles as well, you'll probably see a big speed increase (as long as you're pulling multiple files from each embedded archive).
You might also look at http://truezip.java.net/ I've used an older version of it, and its quite a bit more powerful than the support that's built into Java. I think there is also an Apache Commons library for reading files from within nested archive structures.
I'm trying to create an app and have the ability to save files to /data/data/(packagename)/files or a directory similar to that. The goal would be to have a pdf or doc handler, as necessary, open the files stored on the internal storage and be viewed by the user. I have the code to get a pdf reader that is installed and display the file but I do not know how to package the files so they are installed in a directory like the one above. Also, if I am able to do this would I use getResources to access the files? How should the file structure look in eclipse to make this happen on install of the APK?
I do prefer to have the files stored internally (they are small) and not on the SD card.
I admit I am new to this and am trying to learn as I go. Thanks for the help!
As I understand your approach you only need to place your files to assets folder of your application and then just copy them to the internal storage. Read more here.
I'm facing a problem that, we have a .zip file that contains some text files. Now I'm using java to access that files. If it is not in the .zip file I can read and print on my console easily using FileInputStream.
But how to read a file from .zip file? I use J2SE only..
You should try a ZipInputStream. The interface is a little obtuse, but you can use getNextEntry() to iterate through the items in the .zip file.
As a side note, the Java class-loader does exactly this to load classes from .jar files without extracting them first.
Everything you need is in ZipFile: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipFile.html. Google for examples on the web, and if you have specific problems then come back to SO for help.
(The link will eventually break; when it does simply websearch java zipfile.)