For a project, I am making a fileserver socket. The socket connection works just fine. However, when the Client connects to the server, the server is supposed to pass a string containing all the filenames within a certain directory (in my case -docs/- directory) to the client. Can someone point me in the direction to some helpful code where the filenames are all retrieved and passed to the client as a single string? Thanks for any help!
1 Answer
Use File class to get the list of files from the directory. Iterate through the files to form a string (of file names) that you want to pass back to the client.
Try something on these lines-
final File folder = new File("docs");
final File[] files = folder.listFiles();
final StringBuilder filenames = new StringBuilder();
for(File file : files) {
filenames.append(file.getName());
// append separator if required
}
1 Comment
user2249216
Do you have any code snippets or links to similar problem as this? I haven't found any...