open resource with relative path in Java

I had problems with using the getClass().getResource(“filename.txt”) method. Upon reading the Java docs instructions, if your resource is not in the same package as the class you are trying to access the resource from, then you have to give it relative path starting with “https://stackoverflow.com/”. The recommended strategy is to put your resource files under … Read more

How to read file from res/raw by name

With the help of the given links I was able to solve the problem myself. The correct way is to get the resource ID with getResources().getIdentifier(“FILENAME_WITHOUT_EXTENSION”, “raw”, getPackageName()); To get it as a InputStream InputStream ins = getResources().openRawResource( getResources().getIdentifier(“FILENAME_WITHOUT_EXTENSION”, “raw”, getPackageName()));

Getting android.content.res.Resources$NotFoundException: exception even when the resource is present in android

For my condition the cause was taking int parameter for TextView. Let me show an example int i = 5; myTextView.setText(i); gets the error info above. This can be fixed by converting int to String like this myTextView.setText(String.valueOf(i)); As you write int, it expects a resource not the text that you are writing. So be … Read more

Preferred way of loading resources in Java

Work out the solution according to what you want… There are two things that getResource/getResourceAsStream() will get from the class it is called on… The class loader The starting location So if you do this.getClass().getResource(“foo.txt”); it will attempt to load foo.txt from the same package as the “this” class and with the class loader of … Read more

How to get absolute path to file in /resources folder of your project

The proper way that actually works: URL resource = YourClass.class.getResource(“abc”); Paths.get(resource.toURI()).toFile(); It doesn’t matter now where the file in the classpath physically is, it will be found as long as the resource is actually a file and not a JAR entry. (The seemingly obvious new File(resource.getPath()) doesn’t work for all paths! The path is still … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)