Domanda

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 !!!

È stato utile?

Soluzione

Try converting the URL to its URI equivalent:

FileHelper.read(new File(getClass.getResource("TextFile.rtf").toURI()))

See URL.toURI() for more information.

Altri suggerimenti

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))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top