zoukankan      html  css  js  c++  java
  • Java-异常

    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。如果关闭文件失败,提示关闭文件失败!
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.*;
    public class Main {
    	public static void main(String[] args) {
            byte[] content = null;
            FileInputStream fis = null;
            try {
                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));//打印数组内容
            } catch (FileNotFoundException e) {
                System.out.println("找不到文件testfis.txt,请重新输入文件名");
            } catch (IOException e) {
                System.out.println("打开或读取文件失败!");
            } catch (Exception e)  {
                System.out.println("打开或读取文件失败!");
            }
            finally{
                try {
                    fis.close();
                    System.out.println("关闭文件ing");
                } catch (IOException e) {
                    System.out.println("关闭文件失败!");
                }
            }
    	}
    }
    
    

    6.2 结合题集6-2代码,要将什么样操作放在finally块?为什么?使用finally关闭资源需要注意一些什么?

    • 答:由于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(FileNotFoundException e){
    						System.out.println(e);
    					} catch(IOException e){
    						System.out.println(e);
    						} 
    			System.out.println(Arrays.toString(content));//打印数组内容
    	}
    }
    

    7. 读取文件并组装对象

    • 实验任务书中中的题目3:读取文件并组装对象

    7.1 给出关键代码(需出现你的学号)。额外要求:捕获异常时,将错误的信息按照出错原因:行号:该行内容格式输出。

    //20172112003
    
    public class ReadFileUsingScanner{
        public static void main(String[] args)  throws FileNotFoundException{
            Scanner in = new Scanner(new File("身份证号.txt"));
             int line = 0; //记录行号
                while(in.hasNextLine()){
                    String next_line = in.nextLine();//读出myfile.txt的下一行
                    line++;
                    Scanner lineScanner = new Scanner(next_line);//为每一行建立一个扫描器
                    lineScanner.useDelimiter(" ");//使用空格作为分隔符
                    try {
                        String name = lineScanner.next();//姓名
                        String identity = lineScanner.next();//身份证号
                        String gender = lineScanner.next();//性别
                        String age = lineScanner.next();//年龄
                        String address = lineScanner.next();//地址
                        
                        while(lineScanner.hasNext()){//多个地址
                            address += lineScanner.next();
                        }
                        System.out.println(name+identity+gender+age+address);
                    } catch (Exception e) {
                        System.out.println("第"+ line +"行发生错误:"+ e +" 该行内容为: "+ line );
                    }
                }
            in.close();
        }
    }
    
    
    

    7.2 如果文件有上万行文本,出错的信息可能有很多行,如果将出错信息直接输出到控制台容易被忽略,请问如何解决

    • 答:可以尝试将所有出错信息存入文件中。
  • 相关阅读:
    BZOJ 3252: 攻略(思路题)
    BZOJ 2821: 作诗(Poetize)(分块)
    BZOJ 2597: [Wc2007]剪刀石头布(费用流)
    BZOJ 1565: [NOI2009]植物大战僵尸(网络流+缩点)
    BZOJ 1927: [Sdoi2010]星际竞速(费用流)
    BZOJ 5120: [2017国家集训队测试]无限之环(费用流)
    洛谷 5205 【模板】多项式开根
    LOJ 2737 「JOISC 2016 Day 3」电报 ——思路+基环树DP
    LOJ 2736 「JOISC 2016 Day 3」回转寿司 ——堆+分块思路
    bzoj 2216 [Poi2011]Lightning Conductor——单调队列+二分处理决策单调性
  • 原文地址:https://www.cnblogs.com/lyq622/p/9980244.html
Copyright © 2011-2022 走看看