diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" index 53a7e14d9a8b75466aa1bce95aa031ec418809aa..0e66f33ce2e2437aa0cb805cf43fe2bf0753aa05 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" @@ -225,7 +225,8 @@ "exception.json", "using.json", "runtime_exception.json", - "checked_exception.json" + "checked_exception.json", + "with_resources.json" ], "title": "通过异常处理错误" } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.json" new file mode 100644 index 0000000000000000000000000000000000000000..370b14de69534ab2b85d0c28d5eb10fd522a417a --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "HansBug", + "source": "with_resources.md", + "notebook_enable": false, + "exercise_id": "33e4376c5f924a4f890e6aa10d1c1544" +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.md" new file mode 100644 index 0000000000000000000000000000000000000000..85f301aa948d661d72429734feb2a5ac96b2032f --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/with_resources.md" @@ -0,0 +1,188 @@ +# 资源对象管理 + +现有如下的程序,可以从文件`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); + } + } +} +``` +