From my client class I'll send strings one for the file name, one for the contents.
On the server, how do I make a file from the strings I've received from the client?
Client Class:
String theFile = "TextDoc.txt";
String theFileContent = "text inside TextDoc";
sendToServer.writeBytes(theFile +"\n");
sendToServer.writeBytes(theFileContent);
Server Class:
BufferedReader reFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String theFileFrCl = reFromClient.readLine();
String theFileCtFrCl = reFromClient.readLine();
How can I make a file from what the client has sent? I'm not too sure how to append the content to TextDoc.
Martyn.
On server side we have recieved file name and content :
BufferedReader reFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String theFileFrCl = reFromClient.readLine(); // file name.
String theFileCtFrCl = reFromClient.readLine(); // contents.
After this we'll write file :
FileOutputStream fos = new FileOutputStream(theFileFrCl,true); // opening file in append mode, if it doesn't exist it'll create one.
fos.write(theFileCtFrCl.getBytes()); // write contents to file TextDoc.txt
fos.close();
If you want to return some file's contents back to client just request file name from client as did before :
theFileFrCl = reFromClient.readLine(); // client will write and we'll read line here (file-name).
Now just open file using recieved file name :
try{
FileInputStream fis = new FileInputStream(theFileFrCl);
byte data[] = new byte[fis.available()];
fis.read(data);
fis.close();
toClient.writeBytes(data); // write to client.
}catch(FileNotFoundException fnf)
{
// File doesn't exists with name supplied by client
}