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

📄 servercreatefile.java

📁 一个简单的商品采集系统
💻 JAVA
字号:

//create the data-input-format file

//I wanna create this class as a Panel so it can implements a better use interface
//but it sends the follow error 
//Note: D:\JCreatorV3 LE\MyProjects\DataCollectSys\SRC\ServerCreateFile.java uses or overrides a deprecated API.
//Note: Recompile with -deprecation for details.
//1 error

//notice that:
//the definition in ItemAttribute:
    //type
    // public final int INTEGER = 1, FLOATING = 2, STRING = 3;
    //constrain tag
    //public final boolean CONSTRAIN_TRUE = true, CONSTRAIN_FALSE = false;
    //constrain type that is every integer maping a type of constrain content
    //public final int KEY = 1, ELSE = 2;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ServerCreateFile extends JFrame{
	
   private ObjectOutputStream output;
   private ServerOperatePanel userInterface;
   private JButton enter, open;
   
   public final String attributeNames[] = { "Attribute-Name", "Type","Length",
                                           "ConstrainTag","Constrain"}; 
   public ServerCreateFile()
   {
      super( "The Server Creating a Format-File" );

      getContentPane().setLayout( new BorderLayout() );
      //setLayout( new BorderLayout() );
      
      userInterface = new ServerOperatePanel( 5, attributeNames );
      
      enter = userInterface.getDoTask1();
      enter.setText( "Enter" );
      enter.setEnabled( false );  // disable button to start
      enter.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               addRecord();
            }
         }
      );
      
      addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               if ( output != null ) {
                  addRecord();
                  closeFile();
               }
            }
         }
      );
      
      open = userInterface.getDoTask2();

      open.setText( "Format-File Save As" );
      open.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               openFile();
            }
         }
      );
      getContentPane().add( userInterface, 
                            BorderLayout.CENTER );
      //add( userInterface, BorderLayout.CENTER );
      setSize( 500, 250 );
      show();
   }

   private void openFile()
   {
      JFileChooser fileChooser = new JFileChooser();
      fileChooser.setFileSelectionMode(
         JFileChooser.FILES_ONLY );
      
      int result = fileChooser.showSaveDialog( this );
      // user clicked Cancel button on dialog
      if ( result == JFileChooser.CANCEL_OPTION )
         return;
         
      File 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 {
            output = new ObjectOutputStream(
                        new FileOutputStream( fileName ) );
            open.setEnabled( false );
            enter.setEnabled( true );
         }
         catch ( IOException e ) {
            JOptionPane.showMessageDialog( this,
               "Error Opening File", "Error",
               JOptionPane.ERROR_MESSAGE );
         }      
       }
   }

   private void closeFile() 
   {
      try {
         output.close();
         //System.exit( 0 );
      }
      catch( IOException ex ) {
         JOptionPane.showMessageDialog( this,
            "Error closing file",
            "Error", JOptionPane.ERROR_MESSAGE );
         //System.exit( 1 );
      }
   }

   public void addRecord()
   {
      String fieldValues[] = userInterface.getFieldValues();
     
      // If the account field value is not empty
      if ( ! fieldValues[ 0 ].equals( "" ) ) {
        
         // output the values to the file
         try {   
                
               	 ItemAttribute attribute = new ItemAttribute( fieldValues[ 0 ],
               	                                TypeIndex( fieldValues[ 1 ] ),
               	                                TypeIndex( fieldValues[ 1 ] )==3?Integer.parseInt( fieldValues[ 2 ] ):0,
               	                                ConstrainTagIndex( fieldValues[ 3 ] ),
               	                                ConstrainIndex( fieldValues[ 4 ] ) ); 
                  output.writeObject( attribute );
                  output.flush(); 
                  userInterface.clearFields();          
               
               }
         catch ( NumberFormatException nfe ) {
            JOptionPane.showMessageDialog( this,
               "Invalid enter items",
               "Invalid enter items",
               JOptionPane.ERROR_MESSAGE );
         }
         catch ( IOException io ) {
            closeFile();
         }
      }
   }
   
   //change the input String to the valid data for ItemAttribute
   //ItemAttribute(String,int,int,boolean,int)
   private int TypeIndex( String st)
   {
   	  if( st.equals( "Integer" ) )
   	    return 1;
   	    else if( st.equals( "Float" ) )
   	      return 2;
   	      else if( st.equals( "String" ) )
   	         return 3;
   	         else
   	            return 4;
   	  
   }
   
   private boolean ConstrainTagIndex( String st )
   {
   	  if( st.equals("true") )
   	    return true;
   	    else if( st.equals("false") )
   	      return false;
   	       else
   	         return true;
   	    
   } 
   
   private int ConstrainIndex( String st )
   {
   	 if( st.equals( "Key" ) )
   	   return 1;
   	   else
   	     return 2;
   }
}

⌨️ 快捷键说明

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