Solved

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

Posted on 2004-04-28
29
312 Views
Last Modified: 2010-03-31
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
Comment
Question by:krupini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 11
  • 3
29 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10944292
you mean you want it to append?
I don't think ftp supports that.
0
 

Author Comment

by:krupini
ID: 10944304
Yeah. I want it to keep writting to the same file without having to constantly delete the previous text.
0
 
LVL 92

Expert Comment

by:objects
ID: 10944313
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10944317
You'd have to get the remote file then reupload after appending locally
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10944323
>>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
 

Author Comment

by:krupini
ID: 10944330
ok. So if i find the ftp client which supports appending, Then would i be able to use the same code?
0
 
LVL 92

Expert Comment

by:objects
ID: 10944351
> Then would i be able to use the same code?


no you'd need to change your code to use the new api.
0
 

Author Comment

by:krupini
ID: 10944352
So, is there such thing or not?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10944357
I'll let objects find that for you - since he mentioned it ;-)
0
 

Author Comment

by:krupini
ID: 10944363
Any ideas of a name of an api like that? or where to find it?
0
 

Author Comment

by:krupini
ID: 10944390
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
 
LVL 92

Expert Comment

by:objects
ID: 10944422
> 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
 

Author Comment

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

then the program does not run correctly.
0
 
LVL 92

Expert Comment

by:objects
ID: 10944446
> then the program does not run correctly.

what happens?
0
 

Author Comment

by:krupini
ID: 10944450
it freezes and does write anything to the file
0
 

Author Comment

by:krupini
ID: 10944490
ps: does not
0
 
LVL 92

Expert Comment

by:objects
ID: 10944503
can you post your code
0
 

Author Comment

by:krupini
ID: 10944570
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
 
LVL 92

Expert Comment

by:objects
ID: 10944606
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
 

Author Comment

by:krupini
ID: 10944644
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
 

Author Comment

by:krupini
ID: 10944647
if you already did it can you show me your code?
thanx.
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10944658
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
 

Author Comment

by:krupini
ID: 10944750
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
 
LVL 92

Expert Comment

by:objects
ID: 10944781
> Although it only posts the answers after you call the close function.

try:

         public void writeToFileViaFtp(String output) {
             out.write(output);
             out.flush();
          }
0
 

Author Comment

by:krupini
ID: 10944815
Thanx, but i still need to call the close() method for it to post the results.
0
 

Author Comment

by:krupini
ID: 10945026
Hey!!!, I just got it to work!!!!!!!!!!!
0
 
LVL 92

Expert Comment

by:objects
ID: 10945044
excellent news :)
0
 

Author Comment

by:krupini
ID: 10945048
Thanx man!!!  ;)
0
 
LVL 92

Expert Comment

by:objects
ID: 10945058
No worries :)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 46
jsp insert to database example 2 89
printf performancy 11 104
Eclipse with various Java releases 7 87
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question