程序一直在报NoSuchElementException.看了许久都不知道哪里出错
public static void Body()throws UnknownHostException, IOException {
while(true){
Socket socket=new Socket("localhost",9900);
Scanner in=new Scanner(System.in);
OutputStream os=socket.getOutputStream();
System.out.println("1-Upload File 2-Check File 3-Download 4- ");
int choice=in.nextInt();
os.write(choice);
os.flush();
socket.shutdownOutput();
InputStream is=socket.getInputStream();
int read=is.read();
if(read==1){
ClientAnalyseHandling.sendFile();
}else if(read==2){
}
os.close();
socket.close();
}
}
这是一个功能选择程序,输入1为上传文件,2为查看文件,等等,目前只做好了上传文件功能,如果选择了1则调用ClientAnalyseHandling.sendFile();方法来发送文件。 然后在发送完毕之后我希望程序能够返回功能选择界面,所以我加了while循环。
程序运行后第一次启动功能是可以的,但是第一次完成后开始循环会到功能选择时就开始报错了。
------解决思路----------------------
可能是误操作把System.in关闭了引起的,关闭Scanner会关闭里层的System.in
------解决思路----------------------
检查下什么地方把System.in 关闭了