简体   繁体   中英

Reading a file from server's directory by appending filename to base URL

I have written a method which reads a file from server and it was working fine but now I have modified it to take a filename as parameter and read that file from server. It is a boolean method which returns false if the file was not found on server directory. The problem is, it is catching an exception I don't know what could be the possible reason for it because the file is present on the server directory any help would be grateful.

public final boolean readDataFromServer (String fileName)
{
    try
    {
        String url = "http://example.com/FolderName/".concat(fileName);
        URL webServer = new URL (url);
        Scanner reader = new Scanner(webServer.openStream());

        // reading data in variables by using nextLine() method

        reader.close();
        return true;
     }

     catch (MalformedURLException e)
    {
        return false;
    }
    catch (IOException e)
    {
        JOptionPane.showMessageDialog(null,"The file was not found on the server");
        return false;
    }
    catch (Exception e)     // this is the block where I'm getting an exception
    {
        System.out.println("Exception here");
        return false;

    }
}

EDIT: I tried to print the exception and I got this: "java.util.NoSuchElementException: No line found" while the file present on my server is accessible and has data in it.

It is working fine now. The problem was the loop which was reading the data I called hasNextLine() method from scanner class in the condition and it worked perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM