我们可以使用java在整个计算机中搜索文件名吗?

时间:2015-04-03 10:14:37

标签: java server file-search

我正在创建一个客户端服务器程序,其中客户端向服务器请求文件名,服务器搜索该文件,然后将该文件发送到客户端(如果找到)。在我的情况下,服务器是我的电脑。那么,有没有什么方法可以让我在更短的时间内在整个计算机上搜索该文件。

1 个答案:

答案 0 :(得分:0)

这是java中TCP服务器的示例:

public class TCPServer {

public static void main(String argv[]) {
    String clientSentence;
    //String capitalizedSentence;
    ServerSocket welcomeSocket = null;
    int port = 0;

    try {
        //port = Integer.valueOf(argv[0]);
        port = 6868;
    } catch (ArrayIndexOutOfBoundsException aio) {
        System.out.println("Insert port");
        System.exit(1);
    } catch (NumberFormatException nfe) {
        System.out.println("Argument must be a number");
        System.exit(1);
    }
    try {
        welcomeSocket = new ServerSocket(port);
        System.out.println("started TCP listening on " + String.valueOf(port));
    } catch (IOException ioe) {
        System.out.println("Could not listen on TCP port = " + String.valueOf(port));
        ioe.printStackTrace();
    }

    try {
        if (welcomeSocket != null) {
            while (true) {
                Socket connectionSocket = welcomeSocket.accept();
                System.out.println("connected-----------------------");
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
                while ((clientSentence = inFromClient.readLine()) != null) {
                    System.out.println("File name: " + clientSentence);
                    if (clientSentence.equals("filename")) {
                        outToClient.writeBytes("this file exists\n");
                    }
                }
                inFromClient.close();
                connectionSocket.close();
                System.out.println("Disconnected-------------------");
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            welcomeSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

这是发送文件名和侦听服务器回复的客户端。例如,您通过文件名找到的文件路径:

public class TCPRequester {
public static String sendRequestToServer(String serverIp, int serverPort, String command) throws Exception {

    StringBuilder result = new StringBuilder();
    // Create input and output streams to read from and write to the server

    Socket socket = new Socket(serverIp, serverPort);// Connect to the server
    PrintStream out = new PrintStream(socket.getOutputStream()); // Create output streams to write to the server
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Create input streams to read from the server
    out.write(command.getBytes());

    // Read data from the server until we finish reading the document
    String line;
    while ((line = in.readLine()) != null) {
        result.append(line);
        System.out.println( line );
    }
    System.out.println("finished response");

    // Close our streams
    in.close();
    out.close();
    socket.close();

    return result.toString();
}


public static void main(String[] args) {
    try {
        System.out.println(sendRequestToServer("localhost", 6868, "filename\n"));
    } catch (Exception ex) {
        Logger.getLogger(TCPRequester.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}