Java异常处理和文件I/O
Java Exception Handling and File I/O
[ I am out of ideas I need a to write a code that writes and reads text files within theprocessfiles method and a code that counts and print the total number of borrowers As part of my home I need to write a class that writes and reads text files.
public void
processFiles()throws FileNotFoundException
{
[ I am struggling to write a code that actually reads and writes a text file to a windows explorer folder ]
try
{
System.out.println("Enter your Firstname");
Scanner sc=new Scanner(System.in);
String firstName=sc.nextLine();
System.out.println("Enter your lastname");
String lastName=sc.nextLine();
System.out.println("Enter your library number");
String libraryNumber=sc.nextLine();
System.out.println("Enter the number of books on loan");
int numberOfBooks=sc.nextInt();
System.out.println(firstName +" "+ lastName +" "+ libraryNumber +" "+ numberOfBooks);
int count// I am struggling to to write a code that counts the borrowers and diplay it on the windows page.
int total// I am struggling to to write a code that displays the total number of borrowers to the windows page.
input.close();
output.close();
}
catch (InputMismatchException e) [This is the catch method]
{
System.out.println("Invalid");
}
catch (Exception e) this is the catch method
{
[this is catch statement] System.out。println("错误异常");}
input.close();[ this is the input close]
output.close(); [and output close statements]
}
}
没有人可以为你写完整的代码。然而,这里有一些提示:
保持链接始终打开。
将扫描进程与I/O进程分离;保持try
块尽可能小。
Paths.get()
:
final Path path = Paths.get(someString);
检查文件是否存在,使用:
Files.exists(path);
打开阅读器以文本(非二进制)形式读取文件,使用:
Files.newBufferedReader(path, StandardCharsets.UTF_8);
打开一个写入器将文件写入文本(非二进制),使用:
Files.newBufferedWriter(path, StandardCharsets.UTF_8);
使用try-with-resources语句:
try (
// open I/O resources here
) {
// operate with said resources
} catch (FileSystemException e) {
// fs error: permission denied, file does not exist, others
} catch (IOException e) {
// Other I/O error
}
Java在try块之后(即在第一对花括号之后)为您关闭资源。
相关文章:
- node.js和express中的异常处理
- JavaScript 异常处理未按预期工作
- tomcat服务器停止时UI上的异常处理
- 带异常处理的基数转换
- 使用AJAX上传文件、处理并使用Flask将结果返回到Javascript
- Javascript getJson异常处理
- 在 PHP 头文件中处理特定于页面的 JavaScript
- 在 Java 文件中处理从 Ajax 发送的 FormData
- 使用生成器进行异常处理
- 如何在保留 JavaScript 调试器功能的同时进行优雅的异常处理
- 使用 CSV 文件预处理高图表数据
- 了解 JavaScript 中的异常处理:当更改 try/catch 块的位置时,获得不同的输出
- 如何将多行输出传输到另一个 php 文件以处理数据
- 在单个 PHP 文件上处理多个 ajax
- 在单个 JavaScript 文件中处理多个页面上的事件的正确方法是什么?
- 流星中的异常处理
- 角.如何在视图中显示异常处理程序的错误
- 上传文件时处理网络问题
- 如何使工作 Chrome 扩展程序的未捕获异常处理程序(由于 CORS 保护而不起作用)
- Java异常处理和文件I/O