Read a file using URL - java

I am trying to read a file using URL in java.
FileHelper.read(new File(getClass.getResource("TextFile.rtf")))
I am really confused with the below exception
error: overloaded method constructor File with alternatives:
(java.net.URI)java.io.File <and>
(java.lang.String)java.io.File
cannot be applied to (java.net.URL)
Any idea or suggestion how can I resolve this exception.
Thanks !!!

Try converting the URL to its URI equivalent:
FileHelper.read(new File(getClass.getResource("TextFile.rtf").toURI()))
See URL.toURI() for more information.

All you need to do is to get URI from the URL.
URL url = getClass.getResource("TextFile.rtf");
URI uri = url.toURI();
FileHelper.read(new File(uri))

Related

Convert file path to URI object emf.common.util.URI

I need a solution to convert file path to EMF URI, not a Java URI.
I tried with this:
org.eclipse.emf.common.util.URI ur = org.eclipse.emf.common.util.URI.createURI(URI.createURI(file.getPath()).toString());
...but I get this exception:
java.net.MalformedURLException: unknown protocol: c appears .
Is there another solution?
In EMF, URI classe comes with a lot of static methods to help you create your URI. In your specific case, try URI.createFileURI(...) instead of URI.createURI(...)
URI fileURI = URI.createFileURI(file.getAbsolutePath());
Look here for details about the method: http://download.eclipse.org/modeling/emf/emf/javadoc/2.4.3/org/eclipse/emf/common/util/URI.html#createFileURI(java.lang.String)

android get URL path

I've got a string:
public://imageifarm/3600.jpg
How can I extract the
imageifarm/3600.jpg
Part out using android?
What I've tried so far:
URL drupalQuestionNodeImageURI = new URL("public://imageifarm/3600.jpg");
Log.d("TAG", drupalQuestionNodeImageURI.getPath());
but it throws this exception:
09-16 17:24:39.992: W/System.err(3763): java.net.MalformedURLException: Unknown protocol: public
How can I solve this?
I know I can use regular expressions but that seems to defeat the purpose of URL(URI) in this case.
You should use android.net.Uri
Uri mUri = Uri.parse(public://imageifarm/3600.jpg);
String extract = mUri.getEncodedSchemeSpecificPart();
Use java.net.URI, not java.net.URL.
If you want have to use URL class (when you image sits on Internet) you have to provide valid URL (that begins from valid URL prefix, like http://, https:// etc). In you case you should use Uri class. Uri object can point on files in your local file system. For example:
Uri.fromFile(new File("public://imageifarm/3600.jpg"));

Not too sure how a URI works regarding absolute paths to files

Simple question: why am I getting new IllegalArgumentException: Path component should be '/' when trying to create a zip filesystem at the following URI:
file:E:/somedirectory/somefile
But this seems to work: file:/somedirectory/somefile
What if I have the same paths on two different drives and I need to access a specific one? Or am I completely missing the point of URIs in the first place?
For paths that use windows volumes use the following format:
file:///e:/somedirectory/somefile
The triple /// results from omitting the URL hostname for local files. Compare: file://sometherhost/e:/somedirectory/somefile, which is valid according to the URI spec, if not actually useful for accessing files on remote volumes.
1. Backslashes are used to point directories and files
2. Try it this way...
`E:\\somedirectory\\somefile`
Maybe its easier to do it with the URI builder. I always use it:
URIBuilder builder = new URIBuilder();
builder.setSchema("file").setHost("anyhost").setPath("/yourpath/");
URI uri;
uri = builder.build();
you can check your URI:
System.out.println(uri.toString());
I hope this will help you!

how to replace brackets in url with bracket encoding?

I need a regex pattern that will find and replace brackets in urls to its urls encoding.
For example a base url like:
http://www.mysite.com/bla/blabla/abc[1].txt
will be turned to:
http://www.mysite.com/bla/blabla/abc%5B1%5D.txt
can anyone help please?
EDIT1:
i originaly use commons-httpclient to access this kind of urls.
when I use the first URL I get an "escaped absolute path no valid" exception.
I can't use URLENCODER because when I use it, I get a "host parameter is null" exception.
The following line should do the trick
String s = URLEncoder.encode("http://www.mysite.com/bla/blabla/abc[1].txt", "UTF-8");
Have you tried URLEncoder.encode?
in the java.net.URLEncoder package.
EDIT:
Ok i see... you cannot pass an entire URL to URLEncoder. URLEncoder is mostly used to encode query parameters.
try this instead:
URI uri = new URI("http", "www.mysite.com", "/bla/blabla/abc[1].txt",null);
System.out.println(uri.toASCIIString());

get specific data from URL in android/java?

I have a url like this
https://example.com%3A443:443/www/36d8e8c94aae45048ba9f92f8e62c7b2/file/content?path=%2Fmnt%2Fsdcard%2Fvideo%2Fsample_mpeg4.mp4&yyy=9a7491a1f5664336b5c6bf431f42852c3749ed54722736ec748746858a710c44
I need to get the path of file as /mnt/sdcard/video from the URL . How do i do that ?
I used URL decoder to remove the spaces but i am confused on getting the path of the file .
Thanks for all your help.
Use Uri.getQueryParameter().
import android.net.Uri;
Uri uri = Uri.parse("https://example.com%3A443:443/www/36d8e8c94aae45048ba9f92f8e62c7b2/file/content?path=%2Fmnt%2Fsdcard%2Fvideo%2Fsample_mpeg4.mp4&yyy=9a7491a1f5664336b5c6bf431f42852c3749ed54722736ec748746858a710c44");
String pathParam = uri.getQueryParameter("path");
You can do it with the Uri class. Construct a Uri object and use Uri.getQueryParameter().

Categories