Hello Daniweb!
I have a .txt file on a URL and I'm able to read its contents and display them however and wherever I want in my applet without any issues with this simple code:

    URL url = new URL("http://blabla.com/something.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));

    String str;
    Boolean continueLoop = true;
    while ((str = input.readLine()) != null && continueLoop) {
        String[] fileContents = str.split(" ");
        ... // format and organize the array for display

However my issue is with writing (appending to existing content) to this file.

I've got:

    URL url = new URL("http://blabla.com/something.txt");
    URLConnection connection = url.openConnection();

    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
    out.write("Good morning Starshine!  The earth says HELLOOO!!");

but that's not working. I have a complicated solution working, which is to download the file, apply the changes, then re-upload to the server again, but there's gotta be a better, faster and more efficient way to do this. The file does have read, write and execute permissions.

Help please!

6 Years
Discussion Span
Last Post by bloodbender

As far as I know you can't do what you are trying to do. It is a security issue. To my best knowledge the only way to do it is your complicated solution.


No, it's not a security issue. The code doesn't behave as expected because the way HTTP protocol works.


HTTP protocol deals with resources, not files. Plus, it defines VERBS as part of its specification. When you issue a file read request, what happens under the hood is that the code sends across a "GET" HTTP request which returns the resource present at the location http://blah/something.txt which in this case is a text file. If you need to update the resource, you'll have to satisfy two conditions:

  • Issue a POST HTTP request with the contents of the file as the payload
  • Make sure the server code supports the POST request and knows that it has to update the "text" file

What exactly is this server running? Apache HTTP server? How are you currently uploading the modified files to the server? Can't you do the same with your code?


s.o.s, thanks buddy!

I know php very well so I completely understand your GET and POST explanations, however I'm lost to apply that to java. The server is indeed Apache setup under Linux. A shared server at a hosting company. I'm thinking about making my application like a small desktop app, whose first step on start up is to check for internet connectivity.

Your comments do give me a lot more insight into this though. I'm gonna do some research on POST with java.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.