为什么我会收到一条错误消息;试图从文本文件“读取”时,Java代码中存在未报告的异常Java.io;?

为什么我会收到一条错误消息;试图从文本文件“读取”时,Java代码中存在未报告的异常Java.io;?,java,exception,file-io,try-catch,Java,Exception,File Io,Try Catch,我刚接触java,但仍然有一些问题 我编写了从文本文件读取所有数据的代码。它可以编译,但当我尝试实例化另一个类中的代码时,会出现以下错误: “未报告的异常java.io.fileNotFoundException,必须被捕获或声明为引发” 我知道它们可能是我的抛接球的问题,我有,也没有包括,但我真的不知道如何使用这些,我希望能得到一些帮助 谢谢所有能帮忙的人 这是密码 import java.io.*; import java.io.FileNotFoundException;

我刚接触java,但仍然有一些问题

我编写了从文本文件读取所有数据的代码。它可以编译,但当我尝试实例化另一个类中的代码时,会出现以下错误:

“未报告的异常java.io.fileNotFoundException,必须被捕获或声明为引发”

我知道它们可能是我的抛接球的问题,我有,也没有包括,但我真的不知道如何使用这些,我希望能得到一些帮助

谢谢所有能帮忙的人

这是密码

    import java.io.*;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.*;
    import java.util.Scanner;

    public class readTextFile1
    {
        private static int index = 0;
        private static int numberOfDepartmentsToRead;
        private static int i;

        private static  ArrayList<Employee> allEmployees = new ArrayList<Employee>();


        public readTextFile1()
            throws FileNotFoundException
            {

                Scanner inFile = new Scanner (new File("startup.txt") );

                storageSystem theStorageSystem = new storageSystem();       

                numberOfDepartmentsToRead = inFile.nextInt();

                String depName      = inFile.nextLine();
                System.out.println("this is the first one "+depName);

                while  (index < numberOfDepartmentsToRead ) 
                {

                    String depName1    = inFile.nextLine();
                    System.out.println("this should be the department name"+depName1);
                    String location1     = inFile.nextLine();
                    System.out.println("this should be the location"+location1);
                    String numberOfEmps = inFile.nextLine();
                    int    numberOfEmps1 = Integer.parseInt(numberOfEmps);
                    System.out.println("this is the number of employees: "+numberOfEmps1);
                    Department newDepartment = new Department(depName1 , location1);
                    theStorageSystem.setDepartment(newDepartment);

                    while (i < numberOfEmps1 )
                    {
                        String fName     = inFile.nextLine();
                        System.out.println("his first name is: "+fName);
                        String lName     = inFile.nextLine();
                        System.out.println("his last name is"+ lName);
                        String gender    = inFile.nextLine();
                        System.out.println("his gender is: "+gender);
                        String address   = inFile.nextLine();
                        System.out.println("his adrs is: "+address);
                        String   payLevel  = inFile.nextLine(); 
                        System.out.println("and this is the pay level"+payLevel);
                        int dPayLevel = Integer.parseInt(payLevel);
                        Employee employeesFromList = new Employee(fName, lName, gender, dPayLevel, "1er-543");
                        theStorageSystem.setEmployee(employeesFromList);
                        i++;
                    }   
                    i = 0;
                 index++;   
                }

                        while (inFile.hasNextLine())
                        {
                            String fName     = inFile.nextLine();
                                System.out.println("his first name is: "+fName);
                                String lName     = inFile.nextLine();
                                System.out.println("his last name is"+ lName);
                                String gender    = inFile.nextLine();
                                System.out.println("his gender is: "+gender);
                                String address   = inFile.nextLine();
                                System.out.println("his adrs is: "+address);
                                String   payLevel  = inFile.nextLine(); 
                                System.out.println("and this is the pay level"+payLevel);
                                int dPayLevel = Integer.parseInt(payLevel);
                                Employee employeesFromList = new Employee(fName, lName, gender, dPayLevel, "1er-543");
                                theStorageSystem.setEmployee(employeesFromList);
                            //  allEmployees = theStorageSystem.getEmployee();  
                        }

                        }

                    public ArrayList<Employee> giveEmployeeTf()
                    {
                        return allEmployees;
                    }
        }
import java.io.*;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.util.*;
导入java.util.Scanner;
公共类readTextFile1
{
私有静态int索引=0;
私有静态int numberOfDepartmentsToRead;
私有静态int i;
私有静态ArrayList allEmployees=新ArrayList();
公共readTextFile1()
抛出FileNotFoundException
{
Scanner infle=新扫描仪(新文件(“startup.txt”);
storageSystem存储系统=新存储系统();
numberOfDepartmentsToRead=infle.nextInt();
字符串depName=infle.nextLine();
System.out.println(“这是第一个”+depName);
while(索引
java中的每个moethod都必须声明它可能抛出的异常[除了
RuntimeException
s]。由于您的方法抛出
FileNotFoundException
,因此它必须执行以下两项操作之一:

  • 将其声明为
    抛出FileNotFoundException

  • 使用
    try
    catch
    块,并处理此异常


  • (*)上面提到的是构造函数的激活,即实际激活c'tor的方法。

    您可能应该在try-catch块中使用
    readTextFile1()
    方法:

    readTextFile1 cls = new readTextFile1();
    
    try
    {
        cls.readTextFile1();
    }
    catch (FileNotFoundException e)
    {
        // report error or something
    }
    

    此外,您的类被错误命名,因为它没有描述它的用途;它似乎存储了
    Employee
    对象的列表,并使用此方法读取该数据。您最好调用类
    DepartmentReader
    或其他东西(注意前面的大写字母)。

    如果代码中可能出现异常,您有两个选项:

    • 抓住它们(并有选择地处理它们)或
    • 重播他们
    捕捉它们是通过以try{}开始并以catch(Exc)结束的块来完成的
            public readTextFile1()
                throws FileNotFoundException
    
    readTextFile1 file = new readTextFile1();
    try {
        readTextFile1.readTextFile1();
    } catch (FileNotFoundException ex) {
        // Here, handle the case that the file is not found. For example:
        System.err.println("File not found");
    }