したがって、私の問題は、このリストを.datファイルに入力して読み込みたいのですが、(.datではなく).txtファイルを介してそれを実行しようとしたときに方法がわからないことです。プロジェクトの順序で入力ストリームを読み取れるようにすることで、例外が発生します
.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);
}
}
}