Skip Scanner - 服务器多个操作

时间:2014-03-16 09:36:15

标签: java

我制作了一个允许服务器管理员添加用户(在命令行中)并回答客户订单的服务器。如果服务器管理员没有在命令行中写任何内容并继续接受客户订单,我想知道如何跳过扫描程序。

  while(true){  

                Scanner scan = new Scanner(System.in);
                String user = scan.nextLine();
                BufferedReader in = new BufferedReader(new FileReader(System.getProperty("user.home") + "/SecShareServer/passwords.txt"));
                BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/SecShareServer/passwords.txt",true));
                String line = in.readLine();
                boolean exists = false;
                while(line!=null){
                    if(line.contains(user)){
                        line = line.replace("false", "true");
                        out.write(line);
                        System.out.println("\nUser added.");
                        exists = true;
                    }
                    line=in.readLine();
                    in.close();
                    out.close();
                }
                if(!exists)
                    System.out.println("\nUser doesn't exist.");
                Socket client = socket.accept();
                ThreadClient myThread = new ThreadClient(client);
                Thread thread = new Thread(myThread);
                thread.start();
            }   
  }

1 个答案:

答案 0 :(得分:0)

您可以使用args.length检查是否在命令行中传递了任何参数,如果没有,则可以跳过扫描声明。