我正在尝试打开我的文本文件,但我不知道问题是什么



我正在尝试制作一个程序,其中包含学生ID号,名字和姓氏以及他们的班级成绩。在我希望能够读取数据之后。但是当我尝试读取数据(或打开文件)时出现错误。当我运行该程序时,出现"打开文件时出错"。我有 5 节课,会附上它们。有人可以尝试帮助我并找出问题所在吗?我将附加类:

    public class StudentRecords 
    {
        private int IDnumber;
        private String firstName;
        private String lastName;
        private double grade;
       public StudentRecords()
       {
            this( 0, "", "", 0.0);
        }
        public StudentRecords( int id, String first, String last, double gr )
        {
            setIDnumber( id );
            setFirstName( first);
            setLastName( last );
            setGrade( gr );
        }
        public void setIDnumber( int id)
        {
            IDnumber = id;
        }
        public int getIDnumber()
        {
            return IDnumber;
        }
         public void setFirstName( String first )
        {
            firstName = first;
        }
        public String getFirstName()
        }
            return firstName;
        }
        public void setLastName( String last)
        {
            lastName = last;
        }
        public String getLastName()
        {
            return lastName;
        }
        public void setGrade( double gr)
        {
            grade = gr;
        }
        public double getGrade()
        {
            return grade;
        }
    }

这是第二堂课

    import java.io.FileNotFoundException;
    import java.lang.SecurityException;
    import java.util.Formatter;
    import java.util.FormatterClosedException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    public class StudentTextFile 
    {
        private Formatter output;
        public void openFile()
        {
            try
            {
                output = new Formatter( "students.txt");
            }
            catch ( SecurityException securityException )
            {
                System.err.println(
                    "You do not have write access to this file.");
                System.exit(1);
            }
            catch ( FileNotFoundException fileNotFoundException )
            {
                System.err.println( "Error opening or creating file." );
                System.exit(1);
            }
        }
        public void addStudentRecords()
        {
            StudentRecords record = new StudentRecords();
            Scanner input = new Scanner( System.in);
            System.out.printf("%sn%sn%sn%snn",
            "To terminate input, type the end-of-file indicator",
            "when you are prompted to enter input.",
            "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
            "On Windows type <crtl> z then press enter");
            System.out.printf( "%sn%s",
        "Enter student ID number (> 0), first name, last name and grade. ",
            "? ");
            while ( input.hasNext() )
            {
                try
                {
                    record.setIDnumber( input.nextInt());
                    record.setFirstName( input.next());
                    record.setLastName(input.next());
                    record.setGrade( input.nextDouble());
               if ( record.getIDnumber() > 0 )
               {
                    output.format( "%d %s %s %.2fn", record.getIDnumber(),
                    record.getFirstName(), record.getLastName(), 
                record.getGrade() );
                }
                 else
                {
                System.out.println(
                        "Student ID Number must be greater than 0.");
            }
        }
        catch ( FormatterClosedException formatterClosedException)
        {
            System.err.println( "Error writing to file.");
            return;
        }
        catch ( NoSuchElementException elementException)
        {
            System.err.println( "Invalid input. Please try again.");
            input.nextLine();
        }
        System.out.printf( "%s %s n%s", "Enter student ID number (>0),",
                "first name. last name and grade.", "? ");
    }
}
public void closeFile()
{
    if ( output != null )
        output.close();
}

}

这是第三类

    public class StudentTextFileTest {
        public static void main(String[] args) 
        {
            StudentTextFile application = new StudentTextFile();
            application.openFile();
            application.addStudentRecords();
            application.closeFile();
        }
    }   

这是第四节课

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.lang.IllegalStateException;
    import java.util.NoSuchElementException;
    import java.util.Scanner;
    public class ReadStudentTextFile 
    {
        private Scanner input;
        public void openfile()
        {
            try
            {
                input = new Scanner( new File( "studentrecords.txt"));
            }
            catch ( FileNotFoundException fileNotFoundException )
            {
                System.err.println( "Error opening file.");
                System.exit(1);
            }
        }
        public void readStudentRecords()
        {
            StudentRecords record = new StudentRecords();
            System.out.printf( "%-10s%-12s%-12s%10sn", "Student ID Number",
            "First Name", "Last Name", "Balance");
        try 
        {
            while ( input.hasNext())
            {
                record.setIDnumber( input.nextInt());
                record.setFirstName( input.next() );
                record.setLastName( input.next());
                record.setGrade( input.nextDouble());
                System.out.printf( "%-10d%-12s%-12s%10.2fn",
                record.getIDnumber(), record.getFirstName(),
                record.getLastName(), record.getGrade() );
            }
        }
        catch ( NoSuchElementException elementException )
        {
            System.err.println( "File improperly formed. ");
            input.close();
            System.exit(1);
        }
         catch ( IllegalStateException stateException)
        {
            System.err.println(" Error reading from file.");
            System.exit(1);
        }
        }
        public void closeFile()
        {
            if (input!= null )
                input.close();
        }
     }

这是第五节课

     public class ReadStudentTextFileTest 
    {
        public static void main(String[] args) 
        {
            ReadStudentTextFile application = new ReadStudentTextFile();
            application.openfile();
            application.readStudentRecords();
            application.closeFile();

        }
    }

我第二次尝试放置整个路径。

对于相对路径,如果你在命令行/终端中进行编译和运行,你需要在代码文件的同一目录中.txt studentrecords。

如果您使用的是 IDE,则需要将学生记录.txt放在 src/文件夹下。 这是假设您不在 maven 项目设置中;否则它应该放在/src/main/java/resources/下。

希望这有帮助!

与其每次读取文件时都创建文件,不如尝试使用包装在 BufferedReader 中的 FileReader。

更改此设置:

input = new Scanner( new File( "studentrecords.txt"));

自:

input = new Scanner(new BufferedReader(new FileReader("studentrecords.txt")));

有关详细信息,请参阅扫描程序类的教程。

还要确保您尝试从中读取的文件存在。也就是说,它应该与您的代码位于同一文件夹中。另一个简单的选择是尝试提供文件的完整路径。

相关内容

最新更新