📄 openformatfile.java
字号:
//create the data-input-format file
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OpenFormatFile {
private ObjectInputStream input;
private int attributeNum;
private ItemAttribute attributeArray[];
private final int DEFAULT_NUM = 10;
private int keyIndex;
public final int KEY = 1;
public OpenFormatFile()
{
attributeNum = 0;
attributeArray = new ItemAttribute[ DEFAULT_NUM ];
openFile( );
getFormatFile();
}
private void getFormatFile()
{
ItemAttribute r;
try {
while( true )
{
r = ( ItemAttribute ) input.readObject();//need way to expand the array size
attributeArray[ attributeNum ] = r; //while exception
if( attributeArray[ attributeNum].getConstrain() == KEY )
keyIndex = attributeNum;
attributeNum++;
}
}
catch ( EOFException eofex ) {
//JOptionPane.showMessageDialog( null,
//"No more records in file",
//"End of File", JOptionPane.ERROR_MESSAGE );
}
catch ( ClassNotFoundException cnfex ) {
JOptionPane.showMessageDialog( null,
"Unable to create object",
"Class Not Found", JOptionPane.ERROR_MESSAGE );
}
catch ( IOException ioex ) {
JOptionPane.showMessageDialog( null,
"Error during read from file",
"Read Error", JOptionPane.ERROR_MESSAGE );
}
}
private void openFile( )
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showSaveDialog( null );
// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
return;
File fileName = fileChooser.getSelectedFile();
if ( fileName == null ||
fileName.getName().equals( "" ) )
JOptionPane.showMessageDialog( null,
"Invalid File Name",
"Invalid File Name",
JOptionPane.ERROR_MESSAGE );
else {
// Open the file
try {
input = new ObjectInputStream(
new FileInputStream( fileName ) );
}
catch ( IOException e ) {
JOptionPane.showMessageDialog( null,
"Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE );
}
}
}
public ItemAttribute[] getAttribute()
{
return attributeArray;
}
public int getAttributeNum()
{
return attributeNum;
}
public int getKeyIndex()
{
return keyIndex;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -