0

I've hosted a text file which I would like to load into a string using java.

My code doesn't seem to work producing errors, any help?

try {
    dictionaryUrl = new URL("http://pluginstudios.co.uk/resources/studios/games/hangman/dictionary.dic");
} catch (MalformedURLException catchMalformedURLException) {
    System.err.println("Error 3: Malformed URL exception.\n"
                     + "         Dictionary failed to load.");
}
// 'Dictionary' scanner setting to file
// 'src/Main/Dictionary.dic'
DictionaryS = new Scanner(new File(dictionaryUrl));
System.out.println("Default dictionary loaded.");

UPDATE 1: The file doesn't seem to load going to the catch. But the file exists.

5
  • 1
    Shouldn't you be using a FileInputStream to read from a file? Also, can't you use File just for files in your local file system? Commented Feb 15, 2012 at 12:57
  • I'm having problems with reading local files, it works fine in NetBeans, but after building it, the .jar file doesn't seem to read the local files stored in the .jar file. Hence I'm trying to use a URL instead, I'm not sure how to use FileInputStream? Commented Feb 15, 2012 at 12:59
  • @KristianMatthews: you can't use File or FileInputStream for that matter, to load resources from your jar. Use getClass().getResourceAsStream() instead. Commented Feb 15, 2012 at 13:03
  • @stryba How does this work? My files are stored under src/Main/dictionary.txt Commented Feb 15, 2012 at 13:06
  • I don't know why you wanna load 2.4 mb of text file content into a String. It may take a long time. You should get the data with buffering. Commented Feb 15, 2012 at 13:15

4 Answers 4

1

You could do something that this tutorial does

   public class WebPageScanner {
     public static void main(String[] args) {
       try {
         URLConnection connection =
           new URL("http://java.net").openConnection();
         String text = new Scanner(
           connection.getInputStream()).
           useDelimiter("\\Z").next();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
Sign up to request clarification or add additional context in comments.

1 Comment

What's the useDelimiter("\\Z").next(); do?
0

You need to use HttpClient and retrieve the data as a string or string buffer. then use parse or read as file.

Comments

0

Something like this should work in your case:

DictionaryS = new Scanner(dictionaryUrl.openStream());

1 Comment

please be more specific, what didn't work, what error did you get?
0

JavaDoc tells us:

File(URI uri)

Creates a new File instance by converting the given file: URI into an abstract pathname.

We can't create and use a File instance for any other resource type (like http).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.