异常

异常

2.6.1

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) {
        byte[] content = null;
        FileInputStream fis = null;
        Scanner sc = new Scanner(System.in);//C:\\Users\\小糖人222\\Desktop\\testfis.txt
        while(fis==null) {
        	try {
                String name = sc.next();
                fis = new FileInputStream(name);
                int bytesAvailabe = fis.available();//获得该文件可用的字节数
                if(bytesAvailabe>0){
                    content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
                    fis.read(content);//将文件内容读入数组
                }
                String str = new String(content);
                System.out.println(str);
                System.out.println(Arrays.toString(content));//打印数组内容
            } catch (FileNotFoundException e) {
                System.out.println("找不到文件xxx,请重新输入文件名");
            } catch (IOException e) {
                System.out.println("打开或读取文件失败!");
            } catch (Exception e)  {
                System.out.println("打开或读取文件失败!");
            }
        	finally{
                try {
                	System.out.println("关闭文件ing");
                	if(fis!=null)
                    fis.close();
                    
                } catch (IOException e) {
                    System.out.println("关闭文件失败!");
                }
            }
    	}
        sc.close();
    }
}

2.7.1

package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main2 {
	public static void main(String[] args) {
		Scanner sc = null;
		try {
			sc = new Scanner(new File("‪C:\\Users\\小糖人222\\Desktop\\infomation.txt"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			System.out.println("找不到文件");
		}
        List<User> users = new ArrayList<>();
        int n=0;
		while(sc.hasNextLine()){
		    n++;
	        String line = sc.nextLine();//读出myfile.txt的下一行
	        Scanner lineScanner = new Scanner(line);
	        lineScanner.useDelimiter(" ");
	        User user = null;
	        try {
	            String name = lineScanner.next();//姓名
	            String id = lineScanner.next();//身份证号
	            String gender = lineScanner.next();//性别
	            String age = lineScanner.next();//年龄
	            String address = lineScanner.next();//地址
	            	user = new User(name, id, gender, age, address);
	            users.add(user);
	        }catch(NumberFormatException e0) {
	        	System.out.println("第"+n+"行发生错误: "+e0+" 该行内容为: "+line);
	        }
	        catch(NoSuchElementException e) {
	            System.out.println("第"+n+"行发生错误: "+e+" 该行内容为: "+line);
	        }
	    }
	}
	
}

package work;
public class User {
	private String name;
	private String id;
	private String gender;
	private String age;
	private String address;

	public User(String name, String id, String gender, String age, String address) {
		this.name = name;
		this.id = id;
		this.gender = gender;
		this.age = age;
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}



}

package work;

public class Student {
	private String name;
	private int gradeOfMath;
	
	public int addGrade(int gradeOfMath){
		
		return 0;
	}
	public void setName(String name){
		
		this.name = name;
	}
	public String getName() {
		return name;
	}

	public int getGradeOfMath() {
		return gradeOfMath;
	}
	public void setGradeOfMath(int gradeOfMath) {
		this.gradeOfMath = gradeOfMath;
	}
	
}

posted on 2018-11-21 00:51  黑白熊熊  阅读(80)  评论(0编辑  收藏  举报

导航