Results 1 to 4 of 4
  1. #1
    siteregsam is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Question How to append data to an already existing file?

    hi,

    I am trying to write a server program that will handle multiple clients and receives a file from each client.

    I need to save data from all the clients in a single file on the server.
    I had written a program, but server each time rewrites the content of the file. Instead of rewriting i need the server to append to the file...


    My server code is.....


    import java.io.*;
    import java.net.*;

    public class MultiServerThread_append implements Runnable {

    private Socket connection;
    private int ID;
    public static void main(String[] args) throws IOException{
    try{
    int count = 0;

    ServerSocket socket1 = new ServerSocket(8080);
    System.out.println("MultipleSocketServer Initialized");
    while (true) {
    Socket connection = socket1.accept();
    Runnable runnable = new MultiServerThread(connection, ++count);
    Thread thread = new Thread(runnable);
    thread.start();
    }
    }catch(Exception e){}
    }
    MultiServerThread_append(Socket s, int i){
    this.connection = s;
    this.ID = i;
    }
    public void run(){

    try{
    int filesize = 6022386; //filesize temporary
    int bytesRead;
    int current = 0;

    File clientInfo = new File("D:/filecopy/clientfile_copy.txt");

    byte[] mybytearray = new byte[filesize];
    InputStream is = connection.getInputStream();
    //FileOutputStream fos = new FileOutputStream(clientInfo);
    FileWriter fstream = new FileWriter(clientInfo,true);
    //BufferedOutputStream bos = new BufferedOutputStream(fos);
    BufferedWriter out = new BufferedWriter(fstream);
    bytesRead = is.read(mybytearray, 0 , mybytearray.length);
    current = bytesRead;
    do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    //System.out.println("Inside do loop");
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    String str = new String(mybytearray);
    out.write(str, 0, current);
    out.flush();
    out.close();
    connection.close();
    } catch(IOException e){
    e.printStackTrace();
    }
    }
    }

    Kindly help me with this.
    Thanks in advance....

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by siteregsam View Post
    I need to save data from all the clients in a single file on the server.
    I had written a program, but server each time rewrites the content of the file. Instead of rewriting i need the server to append to the file...
    Read the API documentation for the FileOutputStream class or the FileWriter class. Both have constructors that append data to a (already existing) file.

    kind regards,

    Jos

  3. #3
    siteregsam is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks Jos.
    I used FileOutputStream but my problem remains the same....
    Here is the modified code...

    import java.io.*;
    import java.net.*;

    public class MultiServerThread_append implements Runnable {

    private Socket connection;
    private int ID;
    public static void main(String[] args) throws IOException{
    try{
    int count = 0;

    ServerSocket socket1 = new ServerSocket(8080);
    System.out.println("MultipleSocketServer Initialized");
    while (true) {
    Socket connection = socket1.accept();
    Runnable runnable = new MultiServerThread(connection, ++count);
    Thread thread = new Thread(runnable);
    thread.start();
    }
    }catch(Exception e){}
    }
    MultiServerThread_append(Socket s, int i){
    this.connection = s;
    this.ID = i;
    }
    public void run(){

    try{
    int filesize = 6022386; //filesize temporary
    int bytesRead;
    int current = 0;

    File clientInfo = new File("D:/filecopy/clientfile_copy.txt");

    byte[] mybytearray = new byte[filesize];
    InputStream is = connection.getInputStream();
    FileOutputStream fos = new FileOutputStream(clientInfo, true);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray, 0 , mybytearray.length);
    current = bytesRead;
    do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    //System.out.println("Inside do loop");
    if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);
    bos.write(mybytearray, 0, current);
    bos.flush();
    bos.close();
    connection.close();
    } catch(IOException e){
    e.printStackTrace();
    }
    }
    }

    what to do now?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by siteregsam View Post
    what to do now?
    You have multiple threads writing to the same file simultaneously; that doesn't work. You need a single thread that writes to the file and that makes your program a producer/consumer problem: your producers want to write to a file and a single consumer actually does write to the file. Google can be of help and a bit of java.util.concurrent.* stuff does the rest.

    kind regards,

    Jos
    Last edited by JosAH; 05-03-2010 at 09:12 PM.

Similar Threads

  1. How to Append in file ?
    By Hippo in forum New To Java
    Replies: 2
    Last Post: 03-19-2010, 02:50 PM
  2. Is it possible to insert recorded audio data into an existing file?
    By evan_earnest in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 04:40 AM
  3. Replies: 4
    Last Post: 01-21-2010, 10:31 AM
  4. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 11:17 AM
  5. Run J2ME application using existing jar file
    By chale in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-25-2008, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •