6. 为如下代码加上异常处理
byte[] content = null;
FileInputStream fis = new FileInputStream("testfis.txt");
int bytesAvailabe = fis.available();//获得该文件可用的字节数
if(bytesAvailabe>0){content = new byte[bytesAvailabe];//创建可容纳文件大小的数组fis.read(content);//将文件内容读入数组
}
System.out.println(Arrays.toString(content));//打印数组内容
6.1 改正代码,并增加如下功能。当找不到文件时,需提示用户找不到文件xxx,请重新输入文件名,然后尝试重新打开。 如果是其他异常则提示打开或读取文件失败!。
注1:里面有多个方法均可能抛出异常。
功能2:需要添加finally关闭文件。无论上面的代码是否产生异常,总要提示关闭文件ing。如果关闭文件失败,提示关闭文件失败!
public class Main {public static void main(String[] args) throws IOException {byte[] content = null;FileInputStream fis=null;try {fis = new FileInputStream("d:/testfis.txt");int bytesAvailabe = fis.available();//获得该文件可用的字节数if(bytesAvailabe>0){content = new byte[bytesAvailabe];//创建可容纳文件大小的数组fis.read(content);//将文件内容读入数组}}catch(FileNotFoundException e){System.out.println("找不到文件xxx,请重新输入文件名");}catch(Exception e){System.out.println("打开或读取文件失败!");}finally {try {System.out.println("关闭文件ing");//FileInputStream fis;fis.close();}catch(Exception e){System.out.println("关闭文件失败!");}}System.out.println(Arrays.toString(content));//打印数组内容}}
6.2 结合题集6-2代码,要将什么样操作放在finally块?为什么?使用finally关闭资源需要注意一些什么?
将必须要执行的操作放在finally块,因为finally块中代码一定会执行,注意要进行try catch,因为关闭资源时可能出现异常
6.3 使用Java7中的try-with-resources来改写上述代码实现自动关闭资源。简述这种方法有何好处?
public class Main {public static void main(String[] args) throws IOException {byte[] content = null;try(FileInputStream fis = new FileInputStream("testfis.txt")){int bytesAvailabe = fis.available();//获得该文件可用的字节数if(bytesAvailabe>0){content = new byte[bytesAvailabe];//创建可容纳文件大小的数组fis.read(content);//将文件内容读入数组}}catch(Exception e){System.out.println(e);}System.out.println(Arrays.toString(content));//打印数组内容}
}
使用try-with-resource语句,会自动调用close函数,关闭资源,相比前面的代码,更加简洁,方便
7. 读取文件并组装对象
实验任务书中中的题目3:读取文件并组装对象
7.1 给出关键代码(需出现你的学号)。额外要求:捕获异常时,将错误的信息按照出错原因:行号:该行内容格式输出。
public class ReadFile201721123039 {private static Scanner in;public static void main(String[] args) {// TODO Auto-generated method stubArrayList<User>student=new ArrayList<User>();try{int count=0;Scanner sc = new Scanner(new File("D:/身份信息.txt"));while(sc.hasNextLine()){String line = sc.nextLine();count++;Scanner lineScanner = new Scanner(line);//为每一行建立一个扫描器lineScanner.useDelimiter(" ");try{String a1 = lineScanner.next();//姓名String a2 = lineScanner.next();//身份证号String a3 = lineScanner.next();//性别String a4 = lineScanner.next();//年龄String a5 = lineScanner.next();//地址System.out.println(a1+a2+a3+a4+a5);try {if (a1.length()==0)throw new Exception("姓名为空");if (a2.length()==0||a2.length()!=18)throw new Exception("身份证号格式错误");if (a3.length()==0)throw new Exception("性别未填写");if (!a3.equals("男") && !a3.equals("女")) throw new Exception("性别格式错误");if (a4.length()==0)throw new Exception("年龄未填写");if (a5.length()==0)throw new Exception("地址未填写");} catch (Exception e) {System.out.println(e+":"+count+":"+line);}}catch(Exception e){System.out.println(e+":"+count+":"+line);}}}catch(FileNotFoundException e){System.out.println(e);}Collections.sort(student, (User o1,User o2)->{;return o1.getAge()-o2.getAge();});for(User user:student){System.out.println(user.toString());}}}
7.2 如果文件有上万行文本,出错的信息可能有很多行,如果将出错信息直接输出到控制台容易被忽略,请问如何解决?
将直接输出到控制台改为抛出异常