Does anybody knows how can i read a file without using getAssets();? I've pasted the file in my assets folder because I thought it would be possible to use this method. The problem is that I have to get the file inside a non-activity class and it's not practical to pass on the context as a parameter since I call this class +8x in my code.
I usually code in C# and I'm frustrated because every solution i see uses the GetAssets().
Thanks
Does anybody knows how can i read a file without using getAssets();?
You are not reading a file. You are reading an asset, based on:
I've pasted the file in my assets folder
Assets are files on your development machine. They are not files on the Android device. They are part of your APK, just like resources. And, yes, you need to use getAssets() to be able to access assets.
The problem is that I have to get the file inside a non-activity class
Set up dependency inversion (Dagger, Koin, etc.) and inject a Context into this "non-activity class". In other words, if you do not have access to a Context on which to call getAssets(), that is an architecture problem, not a programming problem.
Related
I'm in doubt about something. I'm developing a Java Desktop application and I have a problem.
I need to get the current local folder that my application (jar file) is in user system. And after this, to search inside of this same folder for some files (like all .txt file, for example). And finally, get the name of only one of this files and converts to string.
Someone can help me?
Antecipate thanks.
May be the getCanonicalPath() and getProperty() functions could help you, since you don't show any code I can't make you any example, but may be looking this could help you a little.
And also there is a similar question here
I feel like this is a very stupid question, but i've looked for 20 minutes now and i can't for the life of me figure it out, since a lot of online solutions to it seem to not be supported anymore.
How do i get a File object of a file that is in a different package?
I need it to be a File object because that's what a need to pass on to another method.
I want to test a method that gets an argument of the type File. The file i want to use to test it is in a different package: puu.sh/8pJAg/b177243091.png (testdata). I can't seem to access it, i keep getting a filenotfoundexception when i do new File("testdata/A000100")
Packages don't contain files, they contain classes and resources. This is because a package doesn't necessarily exist on the filesystem as a directory (it could be part of a .jar file, or it could be downloaded from a network location via any of several protocols, or it could be generated on the fly by a custom classloader -- Java doesn't actually care). Your options seem to be:
Change the method to accept a stream, and use Class.getResourceAsStream(),
Copy the stream to a temporary file, call your method, then delete the temporary file once it is finished, or
Stop storing the data in a package, and make a directory for it somewhere where you can guarantee it will be present.
I'm creating a dynamic web project in Eclipse where I frequently have write and read to and from an XML file. The file is in my project workspace in a folder called xml. I was wondering if Java provided some way to access the file without hard coding the file path. I've been looking around for a while for a solution but I haven't really founding anything that's really clear. Thanks!
You could just drop it in the classpath as suggested by others, but you won't be able to write to it.
Rather supply the absolute path as a VM argument or environment variable so that you don't need to hardcode it.
E.g.
-Dconfig.location=/path/to/config/file
with
File xmlFile = new File(System.getProperty("config.location"), "some.xml");
// ...
As a completely different alternative, you could consider a database.
You can get the proper path using the following from your Servlet:
String filename = getServletContext().getRealPath("/xml/config.xml");
NOTE:
getRealPath may return null if the file is inside a WAR file. In that case, if your file is in WEB_INF/classes, then you could use ServletContext.getResourceAsStream("/config.xml").
See this link:
I don't think the Servlet API gives you anything that would result in a reliable, writable, path to put work files in all containers. If your container runs the WebApp right out of the WAR, getRealPath() couldn't possibly point to something you can actually write to. I think that your only option here that is supported regardless of container is to hard code some path in the web.xml. Do it as a Context Parameter and you may be able to change it at deployment time. At the end of the day, you must declare a fully qualified path in either code or configuration to get the effect you seek.
Alternatively, do you really need to know the name of the file? In some Servlet apps I've managed to get the effect of dynamically writable storage through plain-jane java.io.file.createTempFile: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#createTempFile(java.lang.String, java.lang.String)
How would you go about opening an .xml file that is within a .jar and edit it?
I know that you can do...
InputStream myStream = this.getClass().getResourceAsStream("xmlData.xml");
But how would you open the xmlData.xml, edit the file, and save it in the .jar? I would find this useful to know and don't want to edit a file outside of the .jar... and the application needs to stay running the entire time!
Thank you!
Jar files are just .zip files with different file suffix, and naming convention for contents. So use classes from under java.util.zip to read and/or write contents.
Modifying contents is not guaranteed (or even likely) to effect running system, as class loader may cache contents as it sees fit.
So it might be good to know more about what you are actually trying to achieve with this. Modifying contents of a jar on-the-fly sounds like complicated and error-prone approach...
If you app. has a GUI and you have access to a web site/server, JWS might be the answer. The JNLP API that is available to JWS apps. provides services such as the PersistenceService. Here is a small demo. of the PersistenceService.
The idea would be to check for the XML in the JWS persistence store. If it is not there, write it there, otherwise use the cached version. If it changes, write a new version to the store.
The demo. writes to the store at shut-down, and reads at start-up. But there is no reason it could not be called by a menu item, timer etc.
I'm am creating an Android application, but in order to have one of the functionalities working I need to read a predefined xml file whilst only knowing its name, not the R.id..
In normal Java I know I can use
getClass().getClassLoader().getResource(xmlName)
but using the limited Android SDK thats not working, any knows how to solve this?
Use getResources().getIdentifier() from your Context (e.g., Activity), but please cache the result if you will use it more than once. getIdentifier() is implemented on Resources.
From the Data Storage Section in the android developer manual:
If you have a static file to package with your application at compile time, you can save
the file in your project in res/raw/myDataFile, and then open it with
Resources.openRawResource (R.raw.myDataFile). It returns an InputStream object that you can
use to read from the file.