Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_java
提交
fd3dc8d2
S
skill_tree_java
项目概览
CSDN 技术社区
/
skill_tree_java
通知
43
Star
8
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
fd3dc8d2
编写于
12月 29, 2021
作者:
ccat
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'dev/exception' into 'master'
dev(hansbug): add problem of java try with resources See merge request
!3
上级
114c7e87
144fecc9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
198 addition
and
1 deletion
+198
-1
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
+2
-1
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.json
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.json
+8
-0
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.md
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.md
+188
-0
未找到文件。
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/config.json
浏览文件 @
fd3dc8d2
...
...
@@ -283,7 +283,8 @@
"exception.json"
,
"using.json"
,
"runtime_exception.json"
,
"checked_exception.json"
"checked_exception.json"
,
"with_resources.json"
],
"title"
:
"通过异常处理错误"
,
"keywords_must"
:
[
...
...
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.json
0 → 100644
浏览文件 @
fd3dc8d2
{
"type"
:
"code_options"
,
"author"
:
"HansBug"
,
"source"
:
"with_resources.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"33e4376c5f924a4f890e6aa10d1c1544"
}
\ No newline at end of file
data/1.Java初阶/9.控制执行流程/4.通过异常处理错误/with_resources.md
0 → 100644
浏览文件 @
fd3dc8d2
# 资源对象管理
现有如下的程序,可以从文件
`myfile.txt`
中读取文本内容,将行号以标准的格式输出到标准输出流,对于可能存在的异常情况(如文件不存在、无权限等)需要进行妥善处理,且需要
**保证当文件读取完毕后文件对象被关闭**
,如下所示
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
String
filePath
=
"myfile.txt"
;
InputStream
fileStream
=
null
;
try
{
fileStream
=
new
FileInputStream
(
filePath
);
Scanner
scanner
=
new
Scanner
(
fileStream
);
int
lineno
=
0
;
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
finally
{
if
(
fileStream
!=
null
)
{
fileStream
.
close
();
}
}
}
}
```
请改写上述代码,使用
`try_with_resource`
的方法简化程序,并依然
**确保文件对象在任意情况下均被关闭**
。
因此,下列修改方式正确的是:
## 答案
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
{
String
filePath
=
"myfile.txt"
;
try
(
InputStream
fileStream
=
new
FileInputStream
(
filePath
);
Scanner
scanner
=
new
Scanner
(
fileStream
)
)
{
int
lineno
=
0
;
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
}
}
```
## 选项
### A
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
{
String
filePath
=
"myfile.txt"
;
try
{
InputStream
fileStream
=
new
FileInputStream
(
filePath
);
Scanner
scanner
=
new
Scanner
(
fileStream
);
}
{
int
lineno
=
0
;
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
}
}
```
### B
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
{
String
filePath
=
"myfile.txt"
;
try
(
fileStream
,
scanner
=
new
FileInputStream
(
filePath
),
new
Scanner
(
fileStream
))
{
int
lineno
=
0
;
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
}
}
```
### C
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
{
String
filePath
=
"myfile.txt"
;
try
(
InputStream
fileStream
=
new
FileInputStream
(
filePath
))
{
Scanner
scanner
=
new
Scanner
(
fileStream
);
int
lineno
=
0
;
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
}
}
```
### D
```
java
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Scanner
;
public
class
TestMain
{
public
static
void
main
(
String
[]
args
)
{
String
filePath
=
"myfile.txt"
;
try
(
InputStream
fileStream
=
new
FileInputStream
(
filePath
);
Scanner
scanner
=
new
Scanner
(
fileStream
);
int
lineno
=
0
)
{
while
(
scanner
.
hasNextLine
())
{
lineno
+=
1
;
String
line
=
scanner
.
nextLine
();
System
.
out
.
printf
(
"%3d >>> %s%n"
,
lineno
,
line
);
}
System
.
out
.
println
(
"(EOF)"
);
}
catch
(
IOException
e
)
{
System
.
err
.
printf
(
"Error occurred when reading '%s'.%n"
,
filePath
);
System
.
exit
(-
1
);
}
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录