-1

我正在尝试从文件中搜索命令行中指定的单词。但是,当它正在运行时,它什么也不显示。问题可能是什么?这是我的代码的一部分。感谢你们对我的帮助!

class ClientHandler extends Thread {
        private Socket client;
        private Scanner input;
        private PrintWriter output;
        private ArrayList<String> quotes;
        public ClientHandler3(Socket socket, String file) {
            client = socket;
            try {
                BufferedReader buffer = new BufferedReader(new FileReader(file));
                BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
                String line = reader.readLine();
                try {
                    int ctr = 0;
                    quotes = new ArrayList<String>();
                    while(line != null){
                        quotes.add(ctr, line);

                        ctr++;
                        line = buffer.readLine();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            try {
                input = new Scanner(client.getInputStream());
                output = new PrintWriter(client.getOutputStream(), true);
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }


        public void run(String[] args) {
            String target;
            String message = "";
                target= args[3];
                for(int i = 0; i<quotes.size(); i++){
                    if(quotes.get(i).toUpperCase().contains(target.toUpperCase())){
                        output.println(quotes.get(i));
                    }
                }
                output.println("|");
            try {
                if (client != null) {
                    System.out.println("Closing down connection...");
                    client.close();
                }
            }
            catch(IOException e) {
                System.out.println("Unable to disconnect!");
            }
        }
    }
4

1 回答 1

1

您的客户端读取args[1](主机),args[2](端口),但不是args[3]要搜索的单词。

您的代码也很难阅读,因为它不遵守标准命名约定。类以大写字母开头,而方法以小写字母开头。您的代码执行相反的操作。

于 2013-02-23T15:24:21.553 回答