我正在尝试制作一个程序,其中包含学生的身份证号码,姓名以及他们的班级成绩。之后我想能够读取数据。但是当我尝试读取数据(或打开文件)时,我收到错误。当我运行程序时,我得到"错误打开文件"。我有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("%s\n%s\n%s\n%s\n\n",
"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( "%s\n%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 %.2f\n", 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%10s\n", "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.2f\n",
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();
}
}
答案 0 :(得分:1)
我第二次试图改变整个路径。
对于相对路径,如果您正在命令行/终端中进行编译和运行,则需要将该studentrecords.txt放在代码文件的同一目录中。
如果您正在使用IDE,则需要将studentrecords.txt放在src /文件夹下。这假设您不在maven项目设置中;否则它应该放在/ src / main / java / resources /。
下希望这有帮助!
答案 1 :(得分:0)
您不应每次从中读取文件时创建文件,而应尝试使用包装在BufferedReader中的FileReader。
改变这个:
input = new Scanner( new File( "studentrecords.txt"));
要:
input = new Scanner(new BufferedReader(new FileReader("studentrecords.txt")));
有关详细信息,请参阅tutorial for the Scanner class。
还要确保您尝试读取的文件存在。也就是说,它应该与您的代码位于同一个文件夹中。另一个简单的选择是尝试提供文件的完整路径。