Question

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!

Was it helpful?

Solution

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
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top