मैं निम्न उदाहरण कोड है कहते हैं:scanner.close() क्या करता है?
Scanner scan1 = new Scanner(System.in); // declaring new Scanner called scan1
int x = scan1.nextInt(); // scan for user input and set it to x
System.out.println(x); // print the value of x
scan1.close(); // closes the scanner (I don't know exactly what this does)
Scanner scan2 = new Scanner(System.in); // declaring new Scanner called scan1
int y = scan2.nextInt(); // scan for user input and set it to y
System.out.println(y); // print the value of y
मैं Oracle documentation on the Scanner
class पढ़ सकते हैं और इस में आए:
जब एक स्कैनर बंद कर दिया है, यह अपने इनपुट स्रोत को बंद करता है, तो स्रोत closeable लागू करता होगा इंटरफेस।
यह है कि एक बार एक Scanner
(System.in
का) बंद कर दिया है इसका मतलब यह है, मैं अब पूरे जावा प्रोग्राम के दौरान System.in
उपयोग करने में सक्षम हो जाएगा? या इसका मतलब यह है कि मैं अब कक्षा में इसका उपयोग नहीं कर पाऊंगा? या केवल विधि? या केवल इसका दायरा?
मेरे पास एक और सवाल है, क्या स्कैनर उस दायरे तक सीमित है जो इसे घोषित किया गया था (आदिम डेटा प्रकारों के समान)?
संभावित डुप्लिकेट [सिस्टम.in से जुड़े स्कैनर को बंद करें] (http://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in) – MarsAtomic
इसके अलावा http: // stackoverflow .com/प्रश्न/23621668/जावा-क्लोजिंग-स्कैनर-एंड-रिसोर्स-लीक – NPE
यह भी संबंधित है: http://stackoverflow.com/questions/14962082/close-scanner-without-closing-system-in – NPE