- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一种算法来读取文件并检查用户输入的多个问题。我正在使用 Netbeans,它建议尝试使用资源。我不确定的是文件的关闭。当我第一次创建我的算法时,我把 file.close() 放在了错误的位置,因为它无法到达,因为它前面有一个“return”语句:
while (inputFile.hasNext()) {
String word = inputFile.nextLine();
for (int i = 0; i < sentance.length; i++) {
for (int j = 0; j < punc.length; j++) {
if (sentance[i].equalsIgnoreCase(word + punc[j])) {
return "I am a newborn. Not even a year old yet.";
}
}
}
}
inputFile.close(); // Problem
所以我用这个修复了它:
File file = new File("src/res/AgeQs.dat");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
String word = inputFile.nextLine();
for (int i = 0; i < sentance.length; i++) {
for (int j = 0; j < punc.length; j++) {
if (sentance[i].equalsIgnoreCase(word + punc[j])) {
inputFile.close(); // Problem fixed
return "I am a newborn. Not even a year old yet.";
}
}
}
}
问题是,当我以错误的方式设置它时,Netbeans 建议:
File file = new File("src/res/AgeQs.dat");
try (Scanner inputFile = new Scanner(file)) {
while (inputFile.hasNext()) {
String word = inputFile.nextLine();
for (int i = 0; i < sentance.length; i++) {
for (int j = 0; j < punc.length; j++) {
if (sentance[i].equalsIgnoreCase(word + punc[j])) {
return "I am a newborn. Not even a year old yet.";
}
}
}
}
}
Netbeans 是在更正我的代码,还是只是删除了文件的关闭?这是执行此操作的更好方法吗?我不喜欢使用代码,除非我确切地知道发生了什么。
最佳答案
try-with-resources 保证 AutoCloseable 资源(如 Scanner)将始终关闭。关闭是由 javac 大约隐式添加的。作为
Scanner inputFile = new Scanner(file);
try {
while (inputFile.hasNext()) {
....
}
} finally {
inputFile.close();
}
顺便说一句,您的代码中存在 Netbeans 未能注意到的问题。 Scanner 的方法不会抛出 IOException 但会抑制它。使用 Scanner.ioException 检查读取文件时是否发生异常。
关于java - 使用 try-with-resources 包围扫描仪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078451/
我想在页面中放置这样一个栏: [ Angular -l][变量][装饰][变量][ Angular -r] “可变”间隔部分应该可以调整大小,但最终,周围的容器(包含所有这些)应该规定最大宽度,并且上
我想写一个正则表达式,这样我只匹配没有用方括号括起来的第一个数字。 例如asdadsas,*&(*&(*2asdasd*U(*&*()&(*3应该匹配 2(没有方括号) 和 asdadsas,*&(*
我尝试尝试这些 iostream 奇怪的行为。 int value = 2; cout > 2) > 的性质。在该语言从 C 发展的早期,这些运算符专门用作所谓的“位移位”运算符,其中 > 会将位向右
这个问题在这里已经有了答案: How to use Objective-C classes with names which are keywords in Swift (1 个回答) 关闭 7 年
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
我有一个静态工厂类,如果在调用之前没有进行初始化,它会抛出异常。我不明白为什么 Eclipse 对我大喊大叫,因为我在工厂中抛出了异常(它要求我在 try/catch 中包围抛出异常),但在另一个类中
我有一些代码: public static void a() throws NumberFormatException { return; } public stati
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
This question already has answers here: Using explicitly numbered repetition instead of question mar
包围
在我的网站上,我有一些图像与其余内容存储在相同的 SQL 单元格中。当我加载包含图像的页面并且 PHP 回显“文章”单元格时,图像会自动包含在段落标签中,这不是我想要的。有什么办法可以防止这种情况发生
我想弄清楚为什么我的 JSON 响应被方括号括起来。我正在使用 ASP.NET Web API 和 Angular。我在想这就是我的 Angular 代码没有打印到 HTML 的原因。 namespa
在eclipse中我们可以用try/catch包围一段代码。我想用 if 语句包围一段代码。有没有快捷键。选择代码块后按 Ctrl + 1 没有提示用 If 包围。 最佳答案 突出显示代码块,按 Al
我有一个用 Go 实现的 Web 服务,它从外部服务返回一个 JSON 结构。返回对象后,它看起来像这样: {"otherServiceInfoList":[],"action... 我的 Go 网络
我在一个网站上工作,客户想要类似这样的东西:http://www.csszengarden.com/?cssfile=202/202.css有几个叠加层附加到屏幕的边缘,而中心的文本以原始浏览器滚动条
我想圈出 react-native-vector 图标。我在样式中添加了一个边框半径,但它对所有设备都没有帮助,而且每个图标的行为都不同。 react 原生矢量图标的链接: https://
使用文本节点的以下值... MatcH one MatcHer two MarcH three 如何使用 java matcher.find() 创建以下输出? MatcH one MatcHer t
我有一个读取文件的非常简单的程序。 eclipse 要求我添加 throws 声明或用 try catch 包围。哪一个是“合适的”。以及如果我已经在方法级别 try catch ,为什么还必须拥有其
在我们从“psd2html”服务获得的代码中,我看到围绕 div 标签的内容有很多跨度。 我知道 span 和 div 之间的区别,但我不明白为什么代码看起来像这样: Forgot pass
我有一个 HTML 代码,有 3 个输入字段:
我正在尝试替换字符串中单词 true 的所有实例,例如在 "true && 0 || 1" 中. 这些字符串可能包含变量值,标记为#{varname} .我不想替换被 #{ 包围的实例和 } . 例子
我是一名优秀的程序员,十分优秀!