progy_rock progy_rock - 1 year ago 78
Java Question

Why can't my java code find a text file from the internet?

I made a Java program to read the contents of a text file that I uploaded in the FTP of a test website. The website is MyJavaTest. The text file

is uploaded in the
folder of my website along with the
file. The address of the text file is therefore

I can open the file when I visit the link from my browser (though I see a blank page; I can't see the contents of the file) and I can also perfectly read from the file when it is on my local disk but the problem I am facing is that every time the program throws a runtime

My (free) website host is 000 Web Host. I'm using IntelliJ IDEA IDE. Why can't the program find the file? Please help me solve this issue. I have earlier seen this question(1), this question(2) and this oracle documentation(3) but couldn't solve my problem.

Here is my original code:


public class File_Net{

public static void main(String...args)throws IOException{

URL url = new URL("");
BufferedReader br = null;

try {
br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("File opened successfully\n");
catch(FileNotFoundException fe) {
System.out.println("The file at the specified URL was not found.");

String x;

while((x = br.readLine()) != null){

Every time, I get this output in IntelliJ IDEA

The file at the specified URL wasn't found.

Process finished with exit code 0

Joe Joe
Answer Source

The file is not there, and both Java and your browser receive a 404, with an empty payload:

HTTP/1.0 404 Not Found
Date: Sun, 11 Sep 2016 13:46:57 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 0
Connection: close
Content-Type: text/html

However, your browser doesn't make it as clear that it's received a 404 - use networking tools to see the response code.

You'll need to fix your site.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download