Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a text file directly from Internet using Java?

I am trying to read some words from an online text file.

I tried doing something like this

File file = new File("http://www.puzzlers.org/pub/wordlists/pocket.txt"); Scanner scan = new Scanner(file); 

but it didn't work, I am getting

http://www.puzzlers.org/pub/wordlists/pocket.txt  

as the output and I just want to get all the words.

I know they taught me this back in the day but I don't remember exactly how to do it now, any help is greatly appreciated.

like image 606
randomizertech Avatar asked Jun 06 '11 23:06

randomizertech


People also ask

How do I read a .TXT file in Java?

There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.

How do I read a file in a URL?

After you've successfully created a URL , you can call the URL 's openStream() method to get a stream from which you can read the contents of the URL. The openStream() method returns a java. io. InputStream object, so reading from a URL is as easy as reading from an input stream.

How do you load a file in Java?

Example 1: Java Program to Load a Text File as InputStream txt. Here, we used the FileInputStream class to load the input. txt file as input stream. We then used the read() method to read all the data from the file.


1 Answers

Use an URL instead of File for any access that is not on your local computer.

URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt"); Scanner s = new Scanner(url.openStream()); 

Actually, URL is even more generally useful, also for local access (use a file: URL), jar files, and about everything that one can retrieve somehow.

The way above interprets the file in your platforms default encoding. If you want to use the encoding indicated by the server instead, you have to use a URLConnection and parse it's content type, like indicated in the answers to this question.


About your Error, make sure your file compiles without any errors - you need to handle the exceptions. Click the red messages given by your IDE, it should show you a recommendation how to fix it. Do not start a program which does not compile (even if the IDE allows this).

Here with some sample exception-handling:

try {    URL url = new URL("http://www.puzzlers.org/pub/wordlists/pocket.txt");    Scanner s = new Scanner(url.openStream());    // read from your scanner } catch(IOException ex) {    // there was some connection problem, or the file did not exist on the server,    // or your URL was not in the right format.    // think about what to do now, and put it here.    ex.printStackTrace(); // for now, simply output it. } 
like image 173
Paŭlo Ebermann Avatar answered Sep 24 '22 05:09

Paŭlo Ebermann