How to read and write a file from a server in java?

时间:2019-01-18 18:21:35

标签: java file server

I'm working on a project needing to fetch a file from a server as it would be for a local file. I need to be either able to read it and to write it On the server because I need to save content as data. until now this is what I've tried using an http server.

try {
        URL url = new URL("http://localhost:8080/testServer/hello.html");
        URLConnection conn = url.openConnection();

        File f = new File(url.toURI());

        OutputStream s = new FileOutputStream(f);

        s.write("hello".getBytes());

        s.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

but the logs says that

uri scheme is not file

and I've tried other ways but none gone right.

Ps : don't know if it changes anything but it's an xml file I want to fetch.

0 个答案:

没有答案