Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to append data to an already existing file?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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();
    FileWriter fstream = new FileWriter(clientInfo,true);
    BufferedWriter out = new BufferedWriter(fstream);
    bytesRead = is.read(mybytearray, 0 , mybytearray.length);
    current = bytesRead;
    do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
    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....
    Last edited by JavaPF; May 3rd, 2010 at 01:04 PM. Reason: PLEASE use code tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to append data to an already existing file?

    Try wrapping a FileWriter with a buffered writer:

    BufferedWriter myWriter(new FileWriter("test.txt", true));
    myWriter.write("hello");
    myWriter.close();
    myWriter(new FileWriter("test.txt", true));
    myWriter.write(" world\r\n");
    myWriter.close();

Similar Threads

  1. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 09:10 AM
  2. Retrieving Meta-Data from a File
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: February 9th, 2010, 08:10 AM
  3. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM
  4. How can I append text in JTextArea from another class
    By chikaman in forum AWT / Java Swing
    Replies: 2
    Last Post: December 10th, 2009, 10:26 AM
  5. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 03:39 AM