문제

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!

도움이 되었습니까?

해결책

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
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top