我遇到了一些我试图通过NetBeans 7.3运行的示例代码的问题。我有理由相信它与NetBeans及其配置有关,因为我能够通过Windows 7控制台(CLI)和Eclipse运行此代码而没有任何问题。尽管我喜欢NetBeans我发现自己正在转向看似更有能力的Eclipse,如果不是因为ide代码绘画,我很乐意回到记事本,这无疑会节省大量的时间和压力。
问题是我在尝试读取第一条记录时得到ClassNotFoundException。文件本身似乎打开没有错误。
以下是涉及的问题所涉及的课程--->
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ReadSequentialFile
{
private ObjectInputStream input;
// enable user to select file to open
public void openFile()
{
try // open file
{
input = new ObjectInputStream(
new FileInputStream( "clients.ser" ) );
} // end try
catch ( IOException ioException )
{
System.err.println( "Error opening file." );
} // end catch
} // end method openFile
// read record from file
public void readRecords()
{
AccountRecordSerializable record;
System.out.printf( "%-10s%-12s%-12s%10s\n", "Account",
"First Name", "Last Name", "Balance" );
try // input the values from the file
{
while ( true )
{
record = ( AccountRecordSerializable ) input.readObject();
// display record contents
System.out.printf( "%-10d%-12s%-12s%10.2f\n",
record.getAccount(), record.getFirstName(),
record.getLastName(), record.getBalance() );
} // end while
} // end try
catch ( EOFException endOfFileException )
{
return; // end of file was reached
} // end catch
catch ( ClassNotFoundException classNotFoundException )
{
System.err.printf( "Unable to create object. : %s\n", ClassNotFoundException.getMessage() );
} // end catch
catch ( IOException ioException )
{
System.err.println( "Error during reading from file." );
} // end catch
} // end method readRecords
// close file and terminate application
public void closeFile()
{
try // close file and exit
{
if ( input != null )
input.close();
System.exit( 0 );
} // end try
catch ( IOException ioException )
{
System.err.println( "Error closing file." );
System.exit( 1 );
} // end catch
} // end method closeFile
} // end class ReadSequentialFile
这是NetBeans与--->
有问题的AccountRecordSerializable类import java.io.Serializable;
public class AccountRecordSerializable implements Serializable
{
private int account;
private String firstName;
private String lastName;
private double balance;
// no-argument constructor calls other constructor with default values
public AccountRecordSerializable()
{
this( 0, "", "", 0.0 );
} // end no-argument AccountRecordSerializable constructor
// four-argument constructor initializes a record
public AccountRecordSerializable(
int acct, String first, String last, double bal )
{
setAccount( acct );
setFirstName( first );
setLastName( last );
setBalance( bal );
} // end four-argument AccountRecordSerializable constructor
// set account number
public void setAccount( int acct )
{
account = acct;
} // end method setAccount
// get account number
public int getAccount()
{
return account;
} // end method getAccount
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// get first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// get last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set balance
public void setBalance( double bal )
{
balance = bal;
} // end method setBalance
// get balance
public double getBalance()
{
return balance;
} // end method getBalance
} // end class AccountRecordSerializable
让这一切顺利进行的主要课程: -
public class ReadSequentialFileTest
{
public static void main( String[] args )
{
ReadSequentialFile application = new ReadSequentialFile();
application.openFile();
application.readRecords();
application.closeFile();
} // end main
} // end class ReadSequentialFileTest
我已使用此链接https://netbeans.org/kb/docs/java/project-setup.html#projects-classpath上的建议更改了类路径,以显式使用可找到AccountRecordSerializable的目录。它没有任何修复。
clients.ser包含我放在项目目录根目录中的3条记录。
我讨厌留下未解决的问题,所以我希望有人可以帮助我;)
欢呼咖喱