所以我的问题是,我想在下面的 .dat 文件中输入这个列表并读入,但我不知道如何,当我尝试通过 .txt 文件(而不是 .dat)执行它并将其放入项目订单,所以我可以通过它读取输入流给我和 excpetion
我不介意使用 .txt 但它给了我这个流损坏的异常,如下所示
java.io.StreamCorruptedException:无效的流标头:4C696665
它应该阅读的列表
Life of Pi
Titanic
Tropic Thunder
GoodFellas
Momento
所以我想做的是把上面的列表放在一个inventory.dat文件中,但我不知道怎么做?
这是代码,
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static SortedList inventory;
static BufferedReader in;
public static StockItem getStockItem( String title2 )
{
int idx;
StockItem newItem = new StockItem( title2 );
//
// Determine whether the video with the title is in the inventory.
//
idx = inventory.locateIndex( newItem );
if ( idx <= inventory.size() )
{
StockItem oldItem = (StockItem)inventory.get( idx );
if ( oldItem.compareTo(newItem) == 0 )
return oldItem;
}
//
// If not, insert.
//
inventory.sortedAdd(newItem);
return newItem;
}
//
// If the stock item has no information, remove it from the inventory.
//
public static void VerifyStockItem( StockItem item )
{
if ( item.getHave() == 0 && item.getWant() == 0 &&
item.getWaitingList().isEmpty() )
{
inventory.sortedRemove( item );
}
}
public static void cmd_I( String title )
{
StockItem item = getStockItem( title );
System.out.println( item );
VerifyStockItem( item );
}
public static void cmd_L()
{
System.out.println( inventory );
}
public static void cmd_A( String title )
{
StockItem item = getStockItem( title );
System.out.println( "Input the initial want value:" );
while ( true )
{
try {
item.setWant( Integer.parseInt( in.readLine() ) );
break;
}
catch ( IOException e ) {
continue;
}
}
VerifyStockItem( item );
}
public static void cmd_M( String title )
{
StockItem item = getStockItem( title );
System.out.println( "The original want value was " + item.getWant() +
"." );
System.out.println( "Input the new want value:" );
while ( true )
{
try {
item.setWant( Integer.parseInt( in.readLine() ) );
break;
}
catch ( IOException e ) {
continue;
}
}
VerifyStockItem( item );
}
public static void cmd_D()
{
try {
BufferedReader fin =
new BufferedReader( new FileReader( "incoming.dat" ) );
int lines = Integer.parseInt( fin.readLine() );
for ( int i = 0; i < lines; i++ )
{
StringTokenizer st = new StringTokenizer( fin.readLine() );
String title = st.nextToken();
int count = Integer.parseInt( st.nextToken() );
StockItem item = getStockItem( title );
ListReferenceBased waitingList = item.getWaitingList();
System.out.println ( "Arrival: " + title + " x " + count );
while( count > 0 && !waitingList.isEmpty() )
{
Customer p = (Customer)waitingList.removeFirst();
System.out.println( "" + p + " received the video." );
count--;
}
if ( count > 0 ) {
item.setHave( item.getHave() + count );
}
VerifyStockItem( item );
}
}
catch( FileNotFoundException fnfe ) {
System.out.println( "incoming.dat should exist." );
return;
}
catch( IOException e ) {
System.out.println(
"The operation is aborted due to an IO error." );
}
}
public static void cmd_O()
{
Node iter = inventory.getHead();
while ( iter != null ) {
Node next = iter.getNext();
StockItem item = (StockItem)iter.getItem();
int count = item.getWant()
+ item.getWaitingList().size()
- item.getHave();
if ( count > 0 ) {
System.out.println( item.title + ": " + count );
}
iter = next;
}
}
public static void cmd_R()
{
Node iter = inventory.getHead();
while ( iter != null ) {
Node next = iter.getNext();
StockItem item = (StockItem)iter.getItem();
int count = item.getHave()
- item.getWant()
- item.getWaitingList().size();
if ( count > 0 ) {
System.out.println( item.title + ": " + count + " returned" );
item.setHave( item.getHave() - count );
}
VerifyStockItem( item );
iter = next;
}
}
public static void cmd_S( String title )
{
StockItem item = getStockItem( title );
if ( item.getHave() > 0 )
{
item.setHave( item.getHave() - 1 );
System.out.println( title + ": You took one. " +
item.getHave() + " video(s) left." );
}
else
{
System.out.println( title + " is sold out." );
System.out.println( "Write your name please." );
while (true) {
try {
Customer p = new Customer( in.readLine().trim() );
item.getWaitingList().addLast( p );
break;
}
catch( IOException e )
{
continue;
}
}
}
VerifyStockItem( item );
}
public static void main(String[] args)
{
//
// Loading from the inventory.dat
//
try {
FileInputStream fis = new
FileInputStream("inventory.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
inventory = (SortedList)o;
}
catch (FileNotFoundException fnfe) {
inventory = new SortedList();
}
catch (Exception e) {
System.out.println(e);
}
in = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String cmdLine;
char cmd;
String arg = "";
System.out.print( "Input a command: " );
try {
cmdLine = in.readLine();
}
catch(Exception e) {
break;
}
if (cmdLine.length() == 0) continue;
//
// parse the command line.
//
cmd = cmdLine.charAt(0);
if ( cmdLine.length() >= 3 ) arg = cmdLine.substring(2);
if ( cmd == 'Q' ) break;
//
// dispatch
//
switch( cmd )
{
case 'H':
System.out.println(
"Help on commands \n" +
"---------------- \n" +
"H (help) \n" +
"I <title> (inquire)\n" +
"L (list)\n" +
"A <title> (add)\n" +
"M <title> (modify)\n" +
"D (delivery)\n" +
"O (order)\n" +
"R (return)\n" +
"S <title> (sell)\n" +
"Q (quit)\n" );
break;
case 'I':
cmd_I( arg );
break;
case 'L':
cmd_L();
break;
case 'A':
cmd_A( arg );
break;
case 'M':
cmd_M( arg );
break;
case 'D':
cmd_D();
break;
case 'O':
cmd_O();
break;
case 'R':
cmd_R();
break;
case 'S':
cmd_S( arg );
break;
}
}
//
// Saving into the inventory.dat
//
try {
FileOutputStream fos = new
FileOutputStream("inventory.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(inventory);
fos.close();
// end try
}
catch (Exception e) {
System.out.println(e);
}
}
}