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

📄 createsequentialfile.java

📁 有关文件读写的一个小程序
💻 JAVA
字号:
package com;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import com.BankUI;
import com.AccountRecord;

public class  CreateSequentialFile extends JFrame
{
	private ObjectOutputString output;
	private BankUI userInterface;
	private JButton enterButton,openButton;
	
	public CreateSequentialFile()
	{
		super("Creating a Sequential File of Objects");
		userInterface=new BankUI(4);
		getContentPane().add(userInterface,BordLayout.CENTER);
		openButton=userInterface.getDoTask1Button();
		openButton.setText("Save into File...");
		openButton.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					openFile();
				}
			});
		enterButton=userInterface.getDoTask2Button();
		enterButton.setText("Enter");
		enterButton.setEnabled(false);
		enterButton.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					addRecord();
				}
			});
		addWindowListener(
			new WindowAdaper()
			{
				public void windoeClosing(WindowEvent event)
				{
					if(output!=null)
					addRecord();
					closefile();
				}
			});
		setSize(300,200);
		show();
	}
	public void openFile()
	{
		JFileChooser fileChooser=new JFileChoose();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int result=fileChooser.showSaveDialog(this);
		if(result==JFileChooser.CANCEL_OPTION)
		return;
		File fileName=fileChoose.getSelectedFile();
		if(fileName==NULL||filename.getName().equals(""))
		{
			JOptionPane.showMessageDialog(this,"Invalid File Name",
			"Invalid File Name",JOptionPane.ERROR_MASSAGE);
			
			
		}
		else
		{
			try
			{
				output=new ObjectOutputStream(new FileOutputStream(fileName));
				openButton.setEnabled(false);
				entrButton.setEnablede(true);
			}
			catch(IOException ioException)
			{
				JOptionPane.showMessageDialog(this,
				"Error Opening File","Error",JOptionPane.ERROR_MESSAGE);
				
				
			}
			
		}
		
	}
	private void closeFile()
	{
		try
		{
			output.close();
			System.exit(0);
		}
		catch(IOException ioException)
		{
			JOptionPane.showMessageDialog(this,
			"Error closing file","Error",
			JOptionPane.ERROR_MESSAGE);
		}
	}
	public void addRecord()
	{
		int accountNumber=0;
		AccountRecord record;
		String fieldValues[]=userInteface.getFieldValues();
		if(!fieldValues[BankUI.ACCOUNT].eques(""))
		{
			try
			{
				accountNumber=Integer.parseInt(
					fieldValues[BankUI.ACCOUNT]);
					if(accountNumber>0)
					{
						record=new AccountRecord(accountNumber,fieldValues[
							BankUI.FIRSTNAME],fieldValues[BankUI.LASTNAME],
							Double.parseDouble(fieldValues[BankUI.BALANCE]));
							output.writeObject(record);
							output.flush();
					}
					userInteface.clearFields();
			}
			catch(NumberFormatException formatException)
			{
				JOptionPane.showMessageDialog(this,
				"Bad account number or balance",
				"Invalid Number Format",
				JOptionPane.ERROR_MESSAGE);
			}
			catch(IOException ioException)
			{
				closeFile();
			}
		}
	}
	public static void main(String args[])
	{
		new CreateSequentialFile();
	}
}

⌨️ 快捷键说明

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