Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

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,
0
krupini
Asked:
krupini
  • 15
  • 11
  • 3
1 Solution
 
objectsCommented:
you mean you want it to append?
I don't think ftp supports that.
0
 
krupiniAuthor Commented:
Yeah. I want it to keep writting to the same file without having to constantly delete the previous text.
0
 
objectsCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
You'd have to get the remote file then reupload after appending locally
0
 
CEHJCommented:
>>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
0
 
krupiniAuthor Commented:
ok. So if i find the ftp client which supports appending, Then would i be able to use the same code?
0
 
objectsCommented:
> Then would i be able to use the same code?


no you'd need to change your code to use the new api.
0
 
krupiniAuthor Commented:
So, is there such thing or not?
0
 
CEHJCommented:
I'll let objects find that for you - since he mentioned it ;-)
0
 
krupiniAuthor Commented:
Any ideas of a name of an api like that? or where to find it?
0
 
krupiniAuthor Commented:
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?
0
 
objectsCommented:
> 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.
0
 
krupiniAuthor Commented:
ok but if i don't include the line:
out.close();

then the program does not run correctly.
0
 
objectsCommented:
> then the program does not run correctly.

what happens?
0
 
krupiniAuthor Commented:
it freezes and does write anything to the file
0
 
krupiniAuthor Commented:
ps: does not
0
 
objectsCommented:
can you post your code
0
 
krupiniAuthor Commented:
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.
0
 
objectsCommented:
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);
0
 
krupiniAuthor Commented:
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{}
0
 
krupiniAuthor Commented:
if you already did it can you show me your code?
thanx.
0
 
objectsCommented:
import java.net.*;
import java.io.*;
public class Testing
{
      PrintWriter out = null;

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

          }
          catch (Exception e) {
               e.printStackTrace();
          }
         }
         public void writeToFileViaFtp(String output) {
             out.write(output);
          }

        public void close() throws Exception
        {
           out.close();
        }
}
0
 
krupiniAuthor Commented:
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.
0
 
objectsCommented:
> Although it only posts the answers after you call the close function.

try:

         public void writeToFileViaFtp(String output) {
             out.write(output);
             out.flush();
          }
0
 
krupiniAuthor Commented:
Thanx, but i still need to call the close() method for it to post the results.
0
 
krupiniAuthor Commented:
Hey!!!, I just got it to work!!!!!!!!!!!
0
 
objectsCommented:
excellent news :)
0
 
krupiniAuthor Commented:
Thanx man!!!  ;)
0
 
objectsCommented:
No worries :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 15
  • 11
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now