I/O流及文件

时间:2021-06-19 10:03:24

最近在学I/O流和文件,看了书上的两个小程序,一个是文本文件读写应用小程序,一个是二进制读写应用小程序。

1、文本文件读写应用小程序

package IO流;

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

public class TextFileIO extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTextArea content = new JTextArea();
	JButton read = new JButton("读文件"),write = new JButton("写文件");
	JTextField FileName = new JTextField(20);
	JLabel prompt = new JLabel("输入文件名:",JLabel.RIGHT);
	JPanel commands = new JPanel();
	public TextFileIO(){
		super("文本文件读写示例");           //设置窗体标题
		read.addActionListener(this);
		write.addActionListener(this);
		commands.setLayout(new GridLayout(2,2,1,1));    //设置面板布局
		commands.add(prompt);
		commands.add(FileName);
		commands.add(read);
		commands.add(write);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add("North", commands);
		this.getContentPane().add(new JScrollPane(content));
		this.getContentPane().add("Center", content);
	}
	private void writeTextFile(JTextArea content, String fileName){
		try {
			FileWriter outStream = new FileWriter(fileName);
			outStream.write (content.getText());
			outStream.close();
		} catch (Exception e) {
			content.setText("IOERROR:" + e.getMessage() + "\n");
			e.printStackTrace();
		}
	}

	private void readTextFile(JTextArea content, String fileName){
		try {
			BufferedReader inStream = new BufferedReader(new FileReader(fileName));    //创建并打开流
			String line = inStream.readLine();                 //读一行
			while(line != null){                               //判断是否读完
				content.append(line + '\n');               //显示一行
				line = inStream.readLine();                //读下一行
			}
			inStream.close();                                  //关闭流
		} catch (Exception e) {
			content.setText("IOERROR:" + fileName + "NOT found\n");
			e.printStackTrace();
		}
	}
	public void actionPerformed(ActionEvent evt) {
		String fileName = FileName.getText();
		if (evt.getSource() == read) {
			content.setText("");
			readTextFile(content,fileName);
		} else {
			writeTextFile(content,fileName);
		}
	}
	
	public static void main(String args[]){
		TextFileIO tf1 = new TextFileIO();
		tf1.setSize(400,200);
		tf1.setVisible(true);
		tf1.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
}

运行结果图:

I/O流及文件

2、二进制读写应用小程序

//二进制读写应用小程序
package IO流;

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


public class BinaryFileIO extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea content = new JTextArea();
	private JButton read = new JButton("读二进制文件"), write = new JButton("写二进制文件");
	private JTextField name = new JTextField(10);
	JLabel prompt = new JLabel("输入文件名:",JLabel.RIGHT);
	JPanel commands = new JPanel();
	public BinaryFileIO(){
		super("二进制文件读写示例");
		read.addActionListener(this);
		write.addActionListener(this);
		commands.setLayout(new GridLayout(2,2,1,1));
		commands.add(prompt);
		commands.add(name);
		commands.add(read);
		commands.add(write);
		content.setLineWrap(true);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add("North", commands);
		this.getContentPane().add(new JScrollPane(content));
		this.getContentPane().add("Center", content);
	}
	private void readRecords (String fileName){
		try {
			DataInputStream in =new DataInputStream(new FileInputStream(fileName));
			content.setText("姓名                 年龄                 工资\n");
			try {
				while(true){
					String name = in.readUTF();
					int age = in.readInt();
					double pay = in.readDouble();
					content.append(name + " " + age + " " + pay + "\n");
				}
				}catch(EOFException e){
			} finally {
				in.close();
			}
		}catch(FileNotFoundException e){
			content.setText("IOERROR: File NOT Found: " + fileName + "\n");
		}catch(IOException e){
			content.setText("IOERROR: " + e.getMessage() + "\n");
		}
	}
	private void writeRecords( String fileName ){
		try {
			DataOutputStream out =new DataOutputStream(new FileOutputStream(fileName));
			for(int k = 0; k < 5; k++){
				//String name = "Name" + k;
				out.writeUTF("Name" + k);
				out.writeInt((int)(20 + Math.random() * 25));
				out.writeDouble(2500 + Math.random() * 2000);
			}
			out.close();
		} catch (IOException e) {
			content.setText("ERROR: " + e.getMessage() + "\n");
		}
	}
	public static void main(String[] args) {
		BinaryFileIO bio = new BinaryFileIO();
		bio.setSize(400,200);
		bio.setVisible(true);
		bio.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e) {
		String fileName = name.getText();
		if (e.getSource() == read)
			readRecords(fileName);
		else writeRecords(fileName);
	}
}
运行结果图:

I/O流及文件