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

📄 streamdemo2.java

📁 java程序设计语言源代码
💻 JAVA
字号:
//Example 2 of Chapter 7

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

public class StreamDemo2 extends JFrame
{
	private ScrollPane scrollPane;
	private JTextArea area;
	private JButton newbutton, ranreadbutton, ranwritebutton, readbutton;
	private JPanel panel;
	private RandomAccessFile input, output;
	private int a[] = {1, 2, 3, 4, 5, 6, 7, 8}, aa1 = 2, aa2 = 3;
	private String s[] = {"数学","物理","化学","语文","英语","政治","生物","体育"};
	private String ss1 = "地理", ss2 = "历史";
	private String n[] = {"必修","必修","必修","必修","必修","选修","选修","必修"};
	private String nn1 = "必修", nn2 = "必修";
	private static final int SIZE = 12;
	
	public StreamDemo2()
	{
		super( "随机文件输入/输出演示" );
		getContentPane().setLayout( new BorderLayout() );
		scrollPane = new ScrollPane();
		area = new JTextArea();
		newbutton = new JButton( "建新文件" );
		ranreadbutton = new JButton( "随机读文件" );
		ranwritebutton = new JButton( "随机写文件" );
		readbutton = new JButton( "展示文件内容" );
		
		panel = new JPanel();
		panel.setLayout( new FlowLayout() );
		
		newbutton.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					newFile();
				}
			}
		);
		ranreadbutton.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					randomReadFile();
				}
			}
		);
		ranwritebutton.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					randomWriteFile();
				}
			}
		);
		readbutton.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed( ActionEvent event )
				{
					readFile();
				}
			}
		);
		scrollPane.add( area );
		panel.add( newbutton );
		panel.add( ranreadbutton );
		panel.add( ranwritebutton );
		panel.add( readbutton );
		getContentPane().add( scrollPane, BorderLayout.CENTER );
		getContentPane().add( panel, BorderLayout.NORTH );
		
		setSize( 450, 200 );
		setVisible( true );
	}
	
	private void newFile()
	{
		JFileChooser open = new JFileChooser( new File( "d://" ) );
		open.setFileSelectionMode( JFileChooser.FILES_ONLY );
		int result = open.showSaveDialog( this );
		if( result == JFileChooser.CANCEL_OPTION )
		return;
		File filename = open.getSelectedFile();
		if( filename == null || filename.getName().equals( "" ))
		{
			JOptionPane.showMessageDialog( this, "没有正确选择文件", "错误提示", 
				JOptionPane.ERROR_MESSAGE );
		}
		else
		{
			try{
				if( output == null )output = new RandomAccessFile( filename, "rw" );
				for( int i = 0; i < a.length; i++ )write( output, a[ i ], s[ i ], n[ i ] );
				area.setText( "" );
				output.close();
				output = null;
			}
			catch( IOException ioe)
			{
				JOptionPane.showMessageDialog( this, "保存文件出错", "错误提示", 
					JOptionPane.ERROR_MESSAGE );
			}
		}
	}
	
	private void randomReadFile()
	{
		JFileChooser open = new JFileChooser( new File( "d://" ) );
		open.setFileSelectionMode( JFileChooser.FILES_ONLY );
		int result = open.showOpenDialog( this );
		if( result == JFileChooser.CANCEL_OPTION )
		return;
		File filename = open.getSelectedFile();
		if( filename == null || filename.getName().equals( "" ))
		{
			JOptionPane.showMessageDialog( this, "没有正确选择文件", "错误提示", 
				JOptionPane.ERROR_MESSAGE );
		}
		else
		{
			try{
				if( input == null )input = new RandomAccessFile( filename, "r" );
				input.seek( (5-1)*SIZE );
				area.setText( read( input ) );
				input.close();
				input = null;
			}
			catch( IOException ioe)
			{
				JOptionPane.showMessageDialog( this, "打开文件出错", "错误提示", 
					JOptionPane.ERROR_MESSAGE );
			}
		}
	}
	
	private void randomWriteFile()
	{
		JFileChooser open = new JFileChooser( new File( "d://" ) );
		open.setFileSelectionMode( JFileChooser.FILES_ONLY );
		int result = open.showSaveDialog( this );
		if( result == JFileChooser.CANCEL_OPTION )
		return;
		File filename = open.getSelectedFile();
		if( filename == null || filename.getName().equals( "" ))
		{
			JOptionPane.showMessageDialog( this, "没有正确选择文件", "错误提示", 
				JOptionPane.ERROR_MESSAGE );
		}
		else
		{
			try{
				if( output == null )output = new RandomAccessFile( filename, "rw" );
				output.seek( (aa1-1)*SIZE );
				write( output, aa1, ss1, nn1 );
				output.seek( (aa2-1)*SIZE );
				write( output, aa2, ss2, nn2 );
				area.setText( "" );
				output.close();
				output = null;
			}
			catch( IOException ioe)
			{
				JOptionPane.showMessageDialog( this, "保存文件出错", "错误提示", 
					JOptionPane.ERROR_MESSAGE );
			}
		}
	}
	
	private void readFile()
	{
		JFileChooser open = new JFileChooser( new File( "d://" ) );
		open.setFileSelectionMode( JFileChooser.FILES_ONLY );
		int result = open.showOpenDialog( this );
		if( result == JFileChooser.CANCEL_OPTION )
		return;
		File filename = open.getSelectedFile();
		if( filename == null || filename.getName().equals( "" ))
		{
			JOptionPane.showMessageDialog( this, "没有正确选择文件", "错误提示", 
				JOptionPane.ERROR_MESSAGE );
		}
		else
		{
			try{
				if( input == null )input = new RandomAccessFile( filename, "r" );
				area.setText( "" );
				while( input.getFilePointer() < input.length() )
				{
					area.append( read( input )+"\n" );
				}
				input.close();
				input = null;
			}
			catch( IOException ioe)
			{
				JOptionPane.showMessageDialog( this, "打开文件出错", "错误提示", 
					JOptionPane.ERROR_MESSAGE );
			}
		}
	}
	
	private String read( RandomAccessFile file ) throws IOException
	{
		return ""+file.readInt()+file.readChar()+file.readChar()
			+file.readChar()+file.readChar();
	}
	
	private void write( RandomAccessFile file, int i, String s1, String s2)
		throws IOException
	{
		file.writeInt( i );
		file.writeChars( s1 );
		file.writeChars( s2 );
	}
	
	public static void main(String[] args)
	{
		StreamDemo2 demo = new StreamDemo2();
		demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
}

⌨️ 快捷键说明

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