6. 為如下代碼加上異常處理
6.1 改正代碼,并增加如下功能。當找不到檔案時,需提示使用者找不到檔案xxx,請重新輸入檔案名,然後嘗試重新打開。 如果是其他異常則提示打開或讀取檔案失敗!
修改代碼如下:
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String file;
byte[] content = null;
FileInputStream f = null;
int flag = 1;
while(flag == 1) {
try {
file = sc.next();
f = new FileInputStream(file);
int bytesAvailabe = f.available();//擷取該檔案可用位元組數
if(bytesAvailabe > 0) {
content = new byte[bytesAvailabe];//建立可容納檔案大小的數組
f.read(content);//将檔案内容讀入數組
}
System.out.println(Arrays.toString(content));
}catch(FileNotFoundException e) {
System.out.println("找不到檔案,請重新輸入檔案名");
file = sc.next();
}
catch(IOException e) {
System.out.println("打開或讀取檔案失敗");
}
catch(Exception e) {
System.out.println("打開或讀取檔案失敗");
}
finally {
try {
f.close();
System.out.println("關閉檔案");
}catch(IOException e) {
System.out.println("關閉檔案失敗");
}
}
}
}
}
6.2 結合題集6-2代碼,要将什麼樣操作放在finally塊?為什麼?使用finally關閉資源需要注意一些什麼?
- finally語句必執行,是以finally一般用于釋放資源。因為不論是否發生異常Finally塊中的代碼都會被執行。需要注意的是,使用Finally關閉資源時也有可能發生異常,要對其作try-catch處理。
6.3 使用Java7中的try-with-resources來改寫上述代碼實作自動關閉資源。簡述這種方法有何好處?
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
byte[] content = null;
String file = sc.next();
try(FileInputStream f = new FileInputStream(file)){
int bytesAvailabe = f.available();//擷取該檔案可用位元組數
if(bytesAvailabe > 0) {
content = new byte[bytesAvailabe];//建立可容納檔案大小的數組
f.read(content);//将檔案内容讀入數組
}
System.out.println(Arrays.toString(content));
}catch(FileNotFoundException e) {
System.out.println("找不到檔案,請重新輸入檔案名");
file = sc.next();
}
catch(IOException e) {
System.out.println("打開或讀取檔案失敗");
System.exit(0);
}
}
}
- try-with-resources簡化了代碼,不需要自己進行close()操作,實作了自動關閉資源的功能。
7. 讀取檔案并組裝對象
7.1 給出關鍵代碼(需出現你的學号)。額外要求:捕獲異常時,将錯誤的資訊按照出錯原因:行号:該行内容格式輸出。
代碼
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) throws FileNotFoundException, NoSuchElementException {
int n=0;
ArrayList<User>users=new ArrayList<User>();
Scanner in = new Scanner(new File("D:\\text.txt"));//為myfile.txt這個File建立一個掃描器in
while(in.hasNextLine()){
String line = in.nextLine();//讀出myfile.txt的下一行
n++;
Scanner lineScanner = new Scanner(line);//為每一行建立一個掃描器
lineScanner.useDelimiter(" ");//使用空格作為分隔符
User user = null;
try {
String name = lineScanner.next();//姓名
String id = lineScanner.next();//學号
String age = lineScanner.next();//年齡
user = new User(name,id,age,n);
users.add(user);
}catch(NoSuchElementException e) {
System.out.println(e);
}
}
// Collections.sort(users, new AgeComparator());
for(int i=0;i<users.size();i++)
System.out.println(users.get(i).toString());
}
}
class User{
String name;//姓名
String id;//身份證号
String age;//年齡
int n;
public int getn() {
return n;
}
public void setn(int n) {
this.n=n;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public User(String name,String id,String age,int n) throws NoSuchElementException{
this.name=name;
this.id=id;
this.age=age;
this.n=n;
if(Integer.parseInt(age)<0)
throw new NoSuchElementException("第"+n+"行發生錯誤: "+"年齡小于于0,不正常" );
}
@Override
public String toString() {
return "User [name=" + name+ ", id="+id + ", age=" + age + "]";
}
}
運作結果
7.2 如果檔案有上萬行文本,出錯的資訊可能有很多行,如果将出錯資訊直接輸出到控制台容易被忽略,請問如何解決?
- 解:将所有出錯資訊存入檔案中以便查找。
轉載于:https://www.cnblogs.com/mayifang/p/9980978.html