hasNext() - when does it block and why?

前端 未结 3 1965
无人及你
无人及你 2020-11-29 12:43

I\'m trying to read commands via a Scanner Object. For checking the Input Syntax I use sc.hasNext() (for the case of missing commands). It did work

3条回答
  •  忘掉有多难
    2020-11-29 13:09

    I'm not sure , but the following is my own experience :
    when the Scanner object is fed with a file , it will not be blocking !
    By the term "fed with a file " I mean that the scanner is constructed like this :
    Scanner scanner = new Scanner("myFile.txt");

    But if the scanner is constructed using the getInputStream()method of a Socket object , like this :

    input = socket.getInputStream();
    Scanner scanner = new Scanner(input);
    

    the scanner will be blocking !

提交回复
热议问题