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

📄 fileio.java

📁 文件的输入输出。输入名字
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//:FileIO.java
/**
 *The programme used to realized the I/O operation for the file
 *@author HaiXian Wu in Software College Grade 2003 Class 2  ID:1033710201
 *@version 1.2
 *Program Time:2004-12-3 
 *Modify Time:2004-12-5
 *Modify Time:2004-12-7
 *Modify Time:2004-12-8
 */
 
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import javax.swing.border.*;
 import java.io.*;
 import java.text.*;
 
 public class FileIO extends JFrame
 {
 	private JLabel filePathLabel,
 	               idLabel,nameLabel,scoreLabel,
 	               idTip,nameTip,scoreTip,
 	               totalLabel,averageLabel;
 	private JTextField filePathField,
 	                   idField,nameField,scoreField,
 	                   totalField,averageField;
 	private JButton skimButton,saveButton,averageButton;
 	private Record record = new Record();
 	private MyFile myfile;
 	private boolean isFirstTime = true;  //Whether it is the first time to start the program
 	
 	public FileIO()
 	{
 		   //Create the panel of the part of the file path
 		filePathLabel = new JLabel("文件:");
 		filePathField = new JTextField(20);
 		skimButton = new JButton("浏览");
 		skimButton.setBackground(Color.cyan);
 		
 		JPanel jp_1 = new JPanel();
 		jp_1.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"文件路径"));
 		jp_1.setBackground(Color.orange);
 		jp_1.add(filePathLabel);
 		jp_1.add(filePathField);
 		jp_1.add(skimButton); 		
 		
 		   //Create the panel of the part of the record input
 		idLabel = new JLabel("学号:",JLabel.CENTER);
 		nameLabel = new JLabel("姓名:",JLabel.CENTER);
 		scoreLabel = new JLabel("成绩:",JLabel.CENTER);
 		
        idTip = new JLabel("输入000--100(不包括000)");
        nameTip = new JLabel("字符数不超过20个");
        scoreTip = new JLabel("分数范围:0-100");
        
        idField = new JTextField();
        nameField = new JTextField();
        scoreField = new JTextField();
        
        saveButton = new JButton("保存");
        saveButton.setBackground(Color.cyan);
        
        JPanel jp_2_1 = new JPanel(new GridLayout(3,3));
        jp_2_1.setBackground(Color.orange);
        jp_2_1.add(idLabel);
        jp_2_1.add(idField);
        jp_2_1.add(idTip);
        jp_2_1.add(nameLabel);
        jp_2_1.add(nameField);
        jp_2_1.add(nameTip);
        jp_2_1.add(scoreLabel);
        jp_2_1.add(scoreField);
        jp_2_1.add(scoreTip);
        
        JPanel jp_2_2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
        jp_2_2.setBackground(Color.orange);       
        jp_2_2.add(saveButton);
        
        JPanel jp_2 = new JPanel(new BorderLayout());
        jp_2.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"输入记录"));
        jp_2.setBackground(Color.orange);
        jp_2.add(jp_2_1,BorderLayout.CENTER);
        jp_2.add(jp_2_2,BorderLayout.SOUTH);    
        
           //Create the panel of the part of the average output
        totalLabel = new JLabel("总人数:",JLabel.CENTER);
        averageLabel = new JLabel("平均分:",JLabel.CENTER);
        
        totalField = new JTextField();
        totalField.setEditable(false);
        totalField.setBackground(Color.white);
        
        averageField = new JTextField();
        averageField.setEditable(false);
        averageField.setBackground(Color.white);
        
        averageButton = new JButton("平均分");
        averageButton.setBackground(Color.cyan);
        
        JPanel jp_3_1 = new JPanel(new GridLayout(2,2));
        jp_3_1.setBackground(Color.orange);
        jp_3_1.add(totalLabel);        
        jp_3_1.add(totalField);
        jp_3_1.add(averageLabel);
        jp_3_1.add(averageField);

        JPanel jp_3_2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
        jp_3_2.setBackground(Color.orange);     
        jp_3_2.add(averageButton);
        
        JPanel jp_3 = new JPanel(new BorderLayout());
        jp_3.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,Color.white,Color.black),"平均分"));
        jp_3.setBackground(Color.orange);
        jp_3.add(jp_3_1,BorderLayout.CENTER);
        jp_3.add(jp_3_2,BorderLayout.SOUTH);
        
           //Add the three panels to the frame 
        Container c = getContentPane();        
        c.add(jp_1,BorderLayout.NORTH);
        c.add(jp_2,BorderLayout.CENTER);
        c.add(jp_3,BorderLayout.SOUTH);
        
           //Add listener to the button
        skimButton.addActionListener(new SkimListener());
        saveButton.addActionListener(new SaveListener());
        averageButton.addActionListener(new AverageListener()); 	
 	}
 	
 	   //Inner class to realize the ActionListener of the skimButton
 	class SkimListener implements ActionListener
 	{
 		public void actionPerformed(ActionEvent e)
 		{
	 		JFileChooser jfc = new JFileChooser();
	 		int val = jfc.showOpenDialog(null);
	 		
	 		if (val == JFileChooser.APPROVE_OPTION)	 		
	 		   filePathField.setText(jfc.getSelectedFile().getAbsolutePath());
 		} 		
 	}
 	   //Inner class to realize the ActionListener of the saveButton
 	class SaveListener implements ActionListener
 	{
 		public void actionPerformed(ActionEvent e)
 		{
 			   //If you haven't designate a file in the filepath field
 		   	if (filePathField.getText().trim().length() == 0)
	 		   {
	 		   	  JOptionPane.showMessageDialog(null,"请指定文件!","Error message",JOptionPane.ERROR_MESSAGE);
	 		   	  return;
	 		   }
 			if (!record.setID(idField.getText().trim()) )
 			   idField.setText("");
 			else if (!record.setName(nameField.getText().trim()) )
 			   nameField.setText("");
 			else if (!record.setScore(scoreField.getText().trim()) ) 
 			   scoreField.setText("");
 			else 
 			{      //If the file is not the one you just worked with 
 			       //or it's the first time to start the program,
 			       //you should open a new file!	 		
	 		   	if (isFirstTime == true || myfile.getFilePath() != filePathField.getText().trim())			   
			    {
			   	   myfile = new MyFile(filePathField.getText().trim());
			   	   isFirstTime = false;
			    }
 				    
 				    //Add record into the file
 			    myfile.addRecord(record);
 				   		
 				   //Clear the textfield
 				idField.setText("");
 				nameField.setText("");
 				scoreField.setText("");
 			}              			
 		}
 	}
 	
 	   //Inner class to realize the ActionListener of the averageButton
 	class AverageListener implements ActionListener 
 	{
 		public void actionPerformed(ActionEvent e) 
 		{	 		      
 		   	if (filePathField.getText().trim().length() == 0)
 		   	{
 		   	   JOptionPane.showMessageDialog(null,"请指定文件!","Error message",JOptionPane.ERROR_MESSAGE);
 		   	   return;
 		   	}
 		   	if (isFirstTime == true || myfile.getFilePath() != filePathField.getText().trim())			   
		    {
		   	   myfile = new MyFile(filePathField.getText().trim());
		   	   isFirstTime = false;
		    }
			   	  
	   	    int recordNumber = myfile.getRecordNumber();
	   	  
	   	    if (recordNumber == 0) 
 		    {
 		  	   JOptionPane.showMessageDialog(null,"No records in the file!",
 		  	                               "Error message",JOptionPane.ERROR_MESSAGE);
 		   	   totalField.setText(""); 	       	  
 	       	   averageField.setText("");
 		    }	 		     
 	        else 
 	        {		 	       	  
 	       	   totalField.setText(recordNumber + ""); 	       	  
 	       	   averageField.setText(myfile.getAverage() + "");
 	        } 		   
 		}
    }
    
    public static void main(String args[])
    {
    	try
    	{
    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    	}
    	catch(Exception e)
    	{
    	    e.printStackTrace();	
    	}
    	FileIO frame = new FileIO();
    	
    	frame.setTitle("File I/O");
    	frame.setSize(500,400);
    	frame.setResizable(false);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    }
 }
 
 class MyFile 
 {
 	private int recordNumber = 0;
 	private float totalScore = 0;
 	private String filePath;
 	private RandomAccessFile file;
 	private File f;
 	
 	public MyFile(String path)
 	{
 		filePath = path;
 		f = new File(filePath);
 		
 		if (filePath != null)
 		{			
	 		try
	 		{  
	 		   file = new RandomAccessFile(f,"rw");
	 		     //If the file is empty , initialize it
			   if (file.length() == 0)		 	   	 	   	  
		 	   	  for (int i = 0 ; i < 100 ; i++ )	
		 	      {
		 	   	     file.writeInt(0);
		 	   	     byte[] buf = new byte[20];		 	   	    
		 	   	     file.write(buf);

⌨️ 快捷键说明

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