我正在创建一个类来从文件中读取对象,该对象之前已保存到文件(.txt 格式),但在尝试加载文件时收到以下错误:
java.util.NoSuchElementException
该方法是以这种方式制作的,因此用户应该能够输入文件的名称。即使它已尝试调试 abit 并且也只是使用已经给出的名称引用文件本身(不接受输入)。这是该方法的外观:
public void load( String filename ) throws FileNotFoundException
{
final String FIELD_STOP = ":";
String loadFile;
loadFile = filename +".txt";
FileReader ansfil = new FileReader( loadFile );
Scanner fileIn = new Scanner( ansfil );
fileIn.useLocale( new Locale( "en" ) );
System.out.println(loadFile);
fileIn.useDelimiter( FIELD_STOP );
int size;
try
{
while( fileIn.hasNext() )
{
size = fileIn.nextInt();
archive = new CDarkivImplemented( size );
CD readIn = new CDImplemented();
for( int i = 0; i < size ; i++ )
{
readIn = new CDImplemented();
readIn.title = fileIn.next();
readIn.artist = fileIn.next();
readIn.genre = CD.interpretGenre( fileIn.next() );
readIn.publisher = fileIn.next();
readIn.year = fileIn.nextInt();
readIn.ID = fileIn.nextInt();
archive.addCD(readIn);
}
}
}
catch(Exception e)
{
System.out.println( "Wrong! : " + e );
System.exit(1);
}
fileIn.close();
}