1

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
Martynogea
  • 559
  • 1
  • 6
  • 12
  • [Fastest way to write huge data in text file Java][1] [1]: http://stackoverflow.com/questions/1062113/fastest-way-to-write-huge-data-in-text-file-java – 1218985 Mar 20 '13 at 17:56
  • Check [File](http://docs.oracle.com/javase/7/docs/api/java/io/File.html). – m0skit0 Mar 20 '13 at 17:57

1 Answers1

1

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 
}
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59
  • Where is this stored? If the client wants to get the file back, do we send back toClient.writeBytes(theFileFrCl);? – Martynogea Mar 20 '13 at 18:04
  • This created file will be stored in current working directory in simple words at same location where your class is placed. – VishalDevgire Mar 20 '13 at 18:07
  • To return contents of file first you would ask for file name to return. Then read file and return contents of file. But if you want to immediately return contents after creating file, then Yes : oClient.writeBytes(theFileFrCl); will do. – VishalDevgire Mar 20 '13 at 18:08
  • How can I return the file then? Do I use getName and if matches return it? – Martynogea Mar 20 '13 at 18:10
  • How would I store it in current directory? I already made File cd = new File(".");. is the files created automatically stored in that? Or I have to store it there manually? – Martynogea Mar 20 '13 at 19:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26571/discussion-between-vishald-and-martynogea) – VishalDevgire Mar 20 '13 at 19:22
  • The FileOutputStream will write file in same directory where your code's class is present. No need to do manually. – VishalDevgire Mar 20 '13 at 19:26
  • What if I want to store/move it in another directory? Say, I have another file but that is mpeg, and I want to keep all the media files in another directory. – Martynogea Mar 20 '13 at 19:43
  • 1
    Then you create a media directory in current directory let's say "media" and give it's path to FileOutputStream FileInputStream fis = new FileInputStream("media\\"+theFileFrCl); here media is in current directory if it was in D: drive we'll say FileInputStream("d:\\media\\"+theFileFrCl). – VishalDevgire Mar 20 '13 at 19:48
  • Thanks, the +theFileFrCl was what I was looking for. I didn't it's that easy to store a file in a specific folder. – Martynogea Mar 20 '13 at 19:52