4 Replies Latest reply: Sep 25, 2008 6:44 PM by 843785 RSS

    Read and write txt file from a web directory.

    843785
      I was wondering two things:

      1. I have an application that I'm writing and I need a way to have a txt file on a website that my program can read and then write to. How would i go about hosting this txt file so that my program can access it.

      2. Secondly once i have this file hosted, how would i go about reading and writing to it.

      Thanks
        • 1. Re: Read and write txt file from a web directory.
          843785
          1) Upload it to a remote file host on the internet (You could probably find a few dozen easily with Google, though you'll have to pay for anything worthwhile I would imagine) or run your own web-server application so that you can host it from your own computer.

          2) I've never done this myself, but I would assume you could read from it by pointing to it with a URL object.
          If your application is an Applet at the same place as the file: URL url = new URL(getCodeBase(), "myDocument.txt");
          If not: URL url = new URL("http://www.myWebSite.com/somePath/myDocument.txt");

          As for writing to the file, I'm not sure, unless you have some other way to access it. I would assume the above URL approach wouldn't let you write to it.
          • 2. Re: Read and write txt file from a web directory.
            843785
            OK i was able to read it, by doing what you said. Thanks a lot. Now am trying to figure out how to write back to it. Any ideas.
            • 3. Re: Read and write txt file from a web directory.
              843785
              I just found this page that talks about FTP in Java, which might be what you need. Look toward the end of it especially, where it has the example with the URL object.

              http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html
              • 4. Re: Read and write txt file from a web directory.
                843785
                Well i tried that but it doesn't seem to work. The code is below.
                     public boolean writeToFile()
                     {
                          try{
                                
                                URL url = new URL("http://nath5.hostrator.com/breakFoodFriendzy/Data.txt");
                                URLConnection urlc = url.openConnection();
                                OutputStream os = urlc.getOutputStream();
                                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlc.getOutputStream()));
                          //BufferedWriter bw;
                          //bw = new BufferedWriter(new FileWriter("Data.txt",true));
                          bw.newLine();
                          bw.write("............................................................................................");
                          bw.newLine();
                          bw.write(toString());
                          bw.close();
                          submit.setText("Success");
                          hasSubmited = true;
                          return true;
                          }
                          catch(Exception e){
                               System.out.println("FAILED");
                               return false;
                          }
                     }