⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modify.java

📁 一个简单的商品采集系统
💻 JAVA
字号:
import java.io.*;import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;import javax.swing.*;public class Modify extends JFrame {     //output the data file   private JTextArea recordDisplay;   //operation button   private JButton open, modify, delete, append;   private JPanel buttonPanel;      //output and input stream of storage data file         private ObjectOutputStream output;   private ObjectInputStream input;   //output and input stream of temp data file   private ObjectOutputStream mOutput;   private ObjectInputStream mInput;   //file name   private File fileName;   private File tempFileName;    //a judgement tag   boolean flag;    private int attributeNum;//note the record attribute amount   private String attributeNames[];//note total attribute names of the record   private ItemAttribute attributeArray[];//note total attributes of the record      public final int KEY = 1;//judge wether the attribute is a key   public String key;      public String keyValues[];//to note the key values of records that have input   public int count;//the size of keyValues[]      public OperatePanel userInterface;//operate panel   public JButton enter,openTwo;      public JFrame frame;//the frame when append records      public Modify()   {      getContentPane().setLayout( new BorderLayout() );            buttonPanel = new JPanel();            /////////////////////////////////////////////////////////////////////////      JOptionPane.showMessageDialog( null, "First choose the Format-File");       OpenFormatFile fget = new OpenFormatFile();            attributeNum = fget.getAttributeNum();           attributeArray = new ItemAttribute[ attributeNum ];      attributeArray = fget.getAttribute();            attributeNames = new String[  attributeNum ];      for( int i = 0; i < attributeNum; i++ )         attributeNames[ i ] = attributeArray[ i ].getName();               //JFrame frame = new JFrame ();      frame = new JFrame ();      frame.getContentPane().setLayout( new BorderLayout() );      frame.setSize(300,200 );      userInterface = new OperatePanel( attributeNum, attributeNames );      frame.getContentPane().add( userInterface, BorderLayout.CENTER );            frame.getContentPane().add( enter = new JButton(), BorderLayout.SOUTH );      frame.getContentPane().add( openTwo= new JButton(), BorderLayout.SOUTH );                 // JOptionPane.showMessageDialog( null, "Run here");      frame.addWindowListener(         new WindowAdapter() {            public void windowClosing( WindowEvent e )            {               //readRecords();             }         }      );             /////////////////////////////////////////////////////////////////////////            tempFileName = new File( "TempFile" );      count = 0;      keyValues = new String[ 10 ];            recordDisplay = new JTextArea();      recordDisplay.setEditable( false );      JScrollPane scroller = new JScrollPane( recordDisplay );            getContentPane().add( scroller, BorderLayout.CENTER );////////////////////            //add event listener      //open a data file      open = new JButton( "Open File" );      open.addActionListener(         new ActionListener() {            public void actionPerformed( ActionEvent e )            {               openFile( true );               readRecords();            }         }      );      buttonPanel.add( open );            //modify appointed record      modify = new JButton( "Search" );      modify.addActionListener(         new ActionListener() {            public void actionPerformed( ActionEvent e )            {               //SearchWay();            }         }      );      buttonPanel.add( modify );            //delete appointed record      delete = new JButton( "Delete" );      delete.addActionListener(         new ActionListener() {            public void actionPerformed( ActionEvent e )            {               //DeleteWay();            }         }      );      buttonPanel.add( delete );            //append a record to the  appointed data file        append = new JButton( "Append" );      append.addActionListener(         new ActionListener() {            public void actionPerformed( ActionEvent e )            {                            AppendWay();            }         }      );      buttonPanel.add( append );      getContentPane().add( buttonPanel, BorderLayout.SOUTH );      modify.setEnabled( false );      delete.setEnabled( false );      append.setEnabled( false );            addWindowListener(         new WindowAdapter() {            public void windowClosing( WindowEvent e )            {               System.exit( 0 );            }         }      );             setSize( 400, 500 );      show();   }         private void openFile( boolean firstTime )   {      if ( firstTime ) {         JFileChooser fileChooser = new JFileChooser();                  fileChooser.setFileSelectionMode(            JFileChooser.FILES_ONLY );         int result = fileChooser.showOpenDialog( this );         // user clicked Cancel button on dialog         if ( result == JFileChooser.CANCEL_OPTION )            return;         fileName = fileChooser.getSelectedFile();      }      if ( fileName == null ||           fileName.getName().equals( "" ) )         JOptionPane.showMessageDialog( this,            "Invalid File Name",            "Invalid File Name",            JOptionPane.ERROR_MESSAGE );      else {         // Open the file         try {            // close file from previous operation            if ( input != null )                 input.close();               input = new ObjectInputStream(new FileInputStream ( fileName ) );            //If I call the function readRecords() here             //I will get the mistake:  java.lang.StackOverflowError            //readRecords();            open.setEnabled( false );            modify.setEnabled( true );            delete.setEnabled( true );            append.setEnabled( true );         }         catch ( IOException e ) {            JOptionPane.showMessageDialog( this,               "File does not exist",               "Invalid File Name",               JOptionPane.ERROR_MESSAGE );         }            }   }       private void closeFile()   {      try {         input.close();      }      catch ( IOException ioe ) {         JOptionPane.showMessageDialog( this,            "Error closing file",            "Error", JOptionPane.ERROR_MESSAGE );         //System.exit( 1 );      }   }       private void readRecords()   {            GoodsRecord record;      String nstr="";      String vstr="";            //openFile( false );      try {                    // input the values from the file         boolean playTitle = true;         boolean first = true;                  while ( true ) {            record = ( GoodsRecord ) input.readObject();            GoodsField fieldArray[] = new GoodsField[ attributeNum ];            fieldArray = record.getGoodsRecord();            nstr="";            vstr="";                        int j = 0;            for(;j < attributeNum; j++)               {               	                 	  nstr += ""+fieldArray[ j ].getName()+"\t";               	  vstr += ""+fieldArray[ j ].getValue()+"\t";                }                       if( playTitle )                   {                      recordDisplay.append( "\n"+nstr );                      playTitle = false;                   }            recordDisplay.append( "\n"+vstr );           }                  }      catch ( EOFException eof ) {         closeFile();      }      catch ( ClassNotFoundException cnfex ) {         JOptionPane.showMessageDialog( this,            "Unable to create object",            "Class Not Found", JOptionPane.ERROR_MESSAGE );      }      catch ( IOException e ) {         JOptionPane.showMessageDialog( this,            "Error reading from file",            "Error", JOptionPane.ERROR_MESSAGE );      }   }        private void AppendWay()   {   	   	 GoodsRecord ar;             try{        mOutput = new ObjectOutputStream( new FileOutputStream( tempFileName ) );        input = new ObjectInputStream( new FileInputStream( fileName  ) );    	       	    while ( true ){   	  	  ar = ( GoodsRecord ) input.readObject();   	  	     	  	  GoodsField fieldArray[] = new GoodsField[ attributeNum ];          fieldArray = ar.getGoodsRecord();                   for( int i =0; i < attributeNum; i++)          {         	if( fieldArray[ i ].getConstrain() == KEY )         	  keyValues[ count++ ] = fieldArray[ i ].getValue();          }   	  	  mOutput.writeObject( ar );   	  	  mOutput.flush();   	    }   	       	  }   	  catch ( EOFException eof ){   	 	/*JOptionPane.showMessageDialog( this,               "Out of file",               "Out of file",               JOptionPane.ERROR_MESSAGE );*/   	 }   	 catch ( IOException e ) {            JOptionPane.showMessageDialog( this,               "File does not exist",               "Invalid File Name",               JOptionPane.ERROR_MESSAGE );            }                 catch ( ClassNotFoundException cnfex ) {            JOptionPane.showMessageDialog( this,               "Unable to create object",               "Unable to create object",               JOptionPane.ERROR_MESSAGE );            }    ////////////////////////////////////////////////////////////////////////////                     frame.setVisible(true);       openTwo = userInterface.getDoTask2();               openTwo.setText( "Done" );               openTwo.setEnabled( true );               openTwo.addActionListener(                  new ActionListener() {                     public void actionPerformed( ActionEvent e )                     {                        //readRecords();                      }                  }               );       enter = userInterface.getDoTask1();               enter.setText( "Enter" );               enter.setEnabled( true );  // disable button to start               enter.addActionListener(                  new ActionListener() {                     public void actionPerformed( ActionEvent e )                     {                        appendRecord();                        readRecords();                     }                  }               );   ///////////////////////////////////////////////////////////////////////////////     try{   	      mInput = new ObjectInputStream( new FileInputStream( tempFileName ) );   	      output = new ObjectOutputStream( new FileOutputStream ( fileName ) );   	         	         	      while( true ){   	         GoodsRecord dr2;   	         dr2 = ( GoodsRecord ) mInput.readObject();   	         output.writeObject( dr2 );   	         output.flush();   	      }   	   }	     	      catch ( EOFException eof ){   	 	/*JOptionPane.showMessageDialog( this,               "Out of file",               "Out of file",               JOptionPane.ERROR_MESSAGE );*/   	 }   	 catch ( IOException e ) {            JOptionPane.showMessageDialog( this,               "File does not exist",               "Invalid File Name",               JOptionPane.ERROR_MESSAGE );            }                 catch ( ClassNotFoundException cnfex ) {            JOptionPane.showMessageDialog( this,               "Unable to create object",               "Unable to create object",               JOptionPane.ERROR_MESSAGE );            }   	           }      public void appendRecord()   {      GoodsRecord record;      String fieldValues[] = userInterface.getFieldValues();            try {               boolean flag = true;               GoodsField fieldArray[] = new GoodsField[ attributeNum ];               String addKeyValue = "";                              for( int i = 0; i < attributeNum; i++ )               {               	 fieldArray[ i ] = new GoodsField( attributeArray[ i ], fieldValues[ i ] );               	                   	 if( fieldArray[ i ].getConstrain() == KEY )               	 {               	 	if( fieldArray[ i ].getValue() == null || checkKeyValue( fieldValues[ i ] )== false )                         flag = false;                       else                        addKeyValue = fieldValues[ i ];               	 }               	                     }               if( flag ){               	               	keyValues[ count++ ] = addKeyValue;               	record = new GoodsRecord ( fieldArray );                output.writeObject( record );                output.flush();                  	               }                           else                  JOptionPane.showMessageDialog(null,                                          "The value of KEY attribute wrong!"                                         +"\nInput the record fail!" );              userInterface.clearFields();         }         catch ( NumberFormatException nfe ) {            JOptionPane.showMessageDialog( this,               "Invalid enter items",               "Invalid enter items",               JOptionPane.ERROR_MESSAGE );         }         catch ( IOException io ) {            closeFile();         }         }      private boolean checkKeyValue( String tstr )   {   	 int i = 0;   	 for( ; i < count; i++)   	  if( keyValues[ i ].equals( tstr ) )   	    break;   	 if ( i == count )   	   return true;   	   else   	    return false;    }      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -