Java 使用finally关闭流
我是Java新手,刚刚学到应该在try/catch/finally块中关闭流,但这迫使我在finally包装中使用另一个try-catch,否则我必须抛出异常 这段代码来自一个练习,我不仅要通过它,还要建立良好的编码实践。请告诉我关门的时间好吗?请随意批评代码的其他方面并将其撕碎。这是我好转的唯一途径Java 使用finally关闭流,java,Java,我是Java新手,刚刚学到应该在try/catch/finally块中关闭流,但这迫使我在finally包装中使用另一个try-catch,否则我必须抛出异常 这段代码来自一个练习,我不仅要通过它,还要建立良好的编码实践。请告诉我关门的时间好吗?请随意批评代码的其他方面并将其撕碎。这是我好转的唯一途径 /* Read 2 file names from the console: file1, file2. Write all the bytes in file1 to file2, but i
/*
Read 2 file names from the console: file1, file2.
Write all the bytes in file1 to file2, but in the reverse order.
Close the streams.
Requirements:
1. The program should read a file name twice from the console.
2. Use FileInputStream to read from a file, and use FileOutputStream to write to a file.
3. In the second file, you need to write all the bytes from the first file in the reverse order.
4. The FileInputStream and FileOutputStream must be closed.
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Solution {
private void copyArray () {
FileInputStream input = null;
FileOutputStream output = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a filename to be read: ");
String file1 = reader.readLine();
System.out.print("Enter a filename to write to: ");
String file2 = reader.readLine();
input = new FileInputStream(file1);
output = new FileOutputStream(file2);
byte[] buffer = new byte[input.available()];
int count = input.available();
input.read(buffer);
for (int i = count-1; i>=0; i--) {
output.write(buffer[i]);
}
} catch (IOException e) {
System.out.println("General I/O exception: " + e.getMessage());
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new Solution().copyArray();
}
}
您可以使用try with资源
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
System.out.print("Enter a filename to be read: ");
String file1 = reader.readLine();
System.out.print("Enter a filename to write to: ");
String file2 = reader.readLine();
try(FileInputStream input = new FileInputStream(file1);FileOutputStream output = new FileOutputStream(file2)){
byte[] buffer = new byte[input.available()];
int count = input.available();
input.read(buffer);
for (int i = count-1; i>=0; i--) {
output.write(buffer[i]);
}
}
} catch (IOException e) {
System.out.println("General I/O exception: " + e.getMessage());
}
在发布任何问题之前,请仔细阅读指南。
无论如何,我相信,这将是使用Java特性的正确方法-尝试使用资源
private static void copyArray () {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a filename to be read: ");
String file1 = scanner.next();
System.out.print("Enter a filename to write to: ");
String file2 = scanner.next();
scanner.close();
try (FileInputStream input = new FileInputStream(new File(file1)) ;
FileOutputStream output = new FileOutputStream(new File(file2))){
byte[] buffer = new byte[input.available()];
int count = input.available();
input.read(buffer);
for (int i = count-1; i>=0; i--) {
output.write(buffer[i]);
}
} catch (IOException e) {
System.out.println("General I/O exception: " + e.getMessage());
}
}
如果代码正常工作,并且您希望对其进行改进而不是解决问题,那么这个问题可能更适合。您应该使用。您的学习或教学已经过时几年了。注意:这是对
available()
的滥用。请参阅Javadoc。两次调用它只是要求它在第一次调用和第二次调用之间增加,这将导致代码失败。您还应该对BufferedReader
使用try with resources
。是的,我会更新我的答案。