我现在面临的问题与我的应用程序之一,遇到处理大量交易是需要打开文件和阅读内容时以下异常:我需要在此代码中手动关闭FileInputStream吗?
java.io.FileNotFoundException: /keystore-sample/keystore.jks (Too many open files)
我已经写在Java下面的示例代码:
try {
EncryptDecryptPassword crypt = new EncryptDecryptPassword();
String secretKey = "secret123";
String trustpassencrypted = "ecnrypted1234";
String trustpass = crypt.decryptPassword(trustpassencrypted.trim(), secretKey.trim());
File truststore = new File("/keystore-sample/keystore.jks");
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream(truststore), trustpass.toCharArray());
//Continue with the rest of the code...
} catch (KeyStoreException kse) {
System.out.println("Security configuration failed with the following: " + kse.getCause());
} catch (NoSuchAlgorithmException nsa) {
System.out.println("Security configuration failed with the following: " + nsa.getCause());
} catch (FileNotFoundException fnfe) {
System.out.println("Security configuration failed with the following: " + fnfe.getCause());
} catch (UnrecoverableKeyException uke) {
System.out.println("Security configuration failed with the following: " + uke.getCause());
} catch (CertificateException ce) {
System.out.println("Security configuration failed with the following: " + ce.getCause());
}catch (IOException ioe) {
System.out.println("Security configuration failed with the following: " + ioe.getCause());
}
正如你所看到的,我没有的FileInput流声明一个新的实例,就像下面:
更正:我目的是要表达我没有分配新创建的FileInputStream到一个变量和以后手动关闭它,如下所示:
FileInputStream in = new FileInputStream(truststore);
keyStore.load(in, trustpass.toCharArray());
//once done with the input stream, close it
in.close();
我的问题是:将新的FileInputStream(信任)实际上需要手动关闭,否则将由底层Keystore类来处理?快速浏览Keystore.class的底层反编译代码,我没有看到。只是为了确认这是否是我触及例外的特殊原因。
此外,上面的代码实现被认为是不好的做法?
编辑:
由于对应用环境的一些限制,我运行,这是用旧的Java SE 6u34。
你正在写哪个版本的Java? 1.6,1.7,1.8等...包括那将帮助人们回答你的问题 – Kyrstellaine