Link to home
Start Free TrialLog in
Avatar of krupini
krupini

asked on

The ftp wirting problem is fixed. However there is another problem.

hello, The ftp wirting problem is fixed. However there is another problem.

The programm writes to the file perfectly but only if the file if blank. How can i make it write below the privious text?

i.e a used file

thanx,
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you mean you want it to append?
I don't think ftp supports that.
Avatar of krupini
krupini

ASKER

Yeah. I want it to keep writting to the same file without having to constantly delete the previous text.
you'd need to use an ftp client api (as u were originally) that supports appending.

or u could download the existing file, and upload it again followed by the data you want to append.
You'd have to get the remote file then reupload after appending locally
>>you'd need to use an ftp client api (as u were originally) that supports appending

There isn't such a thing that i've ever seen
Avatar of krupini

ASKER

ok. So if i find the ftp client which supports appending, Then would i be able to use the same code?
> Then would i be able to use the same code?


no you'd need to change your code to use the new api.
Avatar of krupini

ASKER

So, is there such thing or not?
I'll let objects find that for you - since he mentioned it ;-)
Avatar of krupini

ASKER

Any ideas of a name of an api like that? or where to find it?
Avatar of krupini

ASKER

If i put the writting commands inside a loop the it can append.
but if i try calling the same method more than once within the same program then, not

may i know why is that?
> may i know why is that?

cause each connection writes a new file.
You could just keep the connection open and append data to it as required.
Avatar of krupini

ASKER

ok but if i don't include the line:
out.close();

then the program does not run correctly.
> then the program does not run correctly.

what happens?
Avatar of krupini

ASKER

it freezes and does write anything to the file
Avatar of krupini

ASKER

ps: does not
can you post your code
Avatar of krupini

ASKER

this is the important part:

import java.net.*;
import java.io.*;
public class Testing
{
         String output;
         public Testing()
         {
             URLConnection conn;
         }
         public void writeToFileViaFtp(String output) {

          try {
               this.output = output;
               URL ftp = new URL("ftp://try:trytry@69.194.83.97/somefile.txt");
               URLConnection conn = ftp.openConnection();
               conn.setDoOutput(true);
               PrintWriter out = new PrintWriter(conn.getOutputStream());


                   out.write(output);
                   out.close();

          }
          catch (Exception e) {
               e.printStackTrace();
          }
          }
}

This class is called by a GUI. All the GUI does is simulate the throw of a die for an online game. The GUI passes the String value of the throw (to the "output" parameter)and this is supposed to post it on the FTP. The GUI runs fine. But as you might imagine a die has to be thrown more than once and so i need to keep writting to the same file more than once.

thanx.
You would need to open the connection in the ctor, and save the PrintWriter as a member variable.
Then your writeToFileViaFtp would just do

out.write(output);
Avatar of krupini

ASKER

Honestly, that is one of the things i tried but then the "writeToFileViaFtp" would not recognize the out variable, which could not be declared outside beause it has to be inside the try{}
Avatar of krupini

ASKER

if you already did it can you show me your code?
thanx.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krupini

ASKER

Thanx man. Apreciate it. Although it only posts the answers after you call the close function. But i guess that is because of the server and there nothing to be done.
Thanx.
> Although it only posts the answers after you call the close function.

try:

         public void writeToFileViaFtp(String output) {
             out.write(output);
             out.flush();
          }
Avatar of krupini

ASKER

Thanx, but i still need to call the close() method for it to post the results.
Avatar of krupini

ASKER

Hey!!!, I just got it to work!!!!!!!!!!!
excellent news :)
Avatar of krupini

ASKER

Thanx man!!!  ;)
No worries :)