Results 1 to 4 of 4
- 05-03-2010, 06:49 PM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
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....
- 05-03-2010, 06:56 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-03-2010, 07:25 PM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
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?
- 05-03-2010, 08:06 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosLast edited by JosAH; 05-03-2010 at 08:12 PM.
Similar Threads
-
How to Append in file ?
By Hippo in forum New To JavaReplies: 2Last Post: 03-19-2010, 01:50 PM -
Is it possible to insert recorded audio data into an existing file?
By evan_earnest in forum Threads and SynchronizationReplies: 1Last Post: 01-31-2010, 03:40 AM -
Is it possible to insert recorded audio data into an existing file?
By evan_earnest in forum New To JavaReplies: 4Last Post: 01-21-2010, 09:31 AM -
append variables to a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-02-2009, 10:17 AM -
Run J2ME application using existing jar file
By chale in forum CLDC and MIDPReplies: 0Last Post: 08-25-2008, 02:11 PM
Bookmarks