Will the java process keeps the file open if the Scanner was not closed? -


I found out that a Java process throws this error:

  Reason: Java Net.SocketException: Too many open files  

I think it can be improved by increasing the open file range in Unix, however, I want to know why files are not being shut down The first is Java process.

I checked the source code and the snippet given below is used to read data from files.

  string content = new scanner (new file ("../../ example / test.txt"), "UTF-8"). Usage delimiter ("\\ A") Next ();  

I think there is a problem because the scanner is not being closed after reading? Can this be the problem? Or because a scanner object is not created, do not need to close it?

I think what's the problem here because the scanner should not be closed after reading Used to be?

This is likely to be the most culprit if a garbage is available for storage then a scanner can be turned off. However, since the garbage collector will run it is impossible to predict, so you should not trust it. Instead, you should explicitly close the scanner when you terminate it means that you should follow this pattern:

  Scanner scan = tap; {Scan = try new scanner (file); String token = scan.next (); // Do something with token} Catch (Exception pre) {// print out error message} Finally scan.close (); }  

Of course, depending on your exact requirements, it can be in a method that promotes exceptions by declaring only a throwing clause. The main idea is to ensure that the scanner is closed or not, the exception is never dropped, finally the from anywhere section is required.

Note that Java 7 has introduced "try with resources" so that it becomes easy to manage this kind of situation. If you are using Java 7, then I suggest that you take a look at this.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -