I/O流及文件
来源:互联网 发布:肯尼迪遇刺档案 知乎 编辑:程序博客网 时间:2024/05/22 13:30
最近在学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);}});}}
运行结果图:
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);}}运行结果图:
阅读全文
1 0
- I/O流及文件
- 文件I/O流
- I/O :文件流
- Java中的I/O流及利用I/O流实现文件复制功能
- java I/O流 文件的生成及筛选
- 第19章 文件及I/O
- 文件和I/O流
- 文件和I/O流
- I/O流与文件
- I/O流 文件读写
- 文件I/O流读写
- I/O流,(文件)
- 文件 I/O字节流
- 文件 I/O字符流
- 文件 I/O缓冲流
- Unix编程:文件I/O操作及文件描述符
- Linux I/O及 I/O Cache
- 文件I/O与标准I/O
- JS放大图片
- 1.SparkContext源码分析
- 多线程编程之pthread_create pthread_join pthread_attr_t
- Invalidate的作用
- MATLAB学习笔记 彩色空间转换
- I/O流及文件
- 测试小故事32:只是好那么一点点
- 开源框架推荐:网速监听—facebook/network-connection-class
- Leetcode 13 Roman to Integer
- 并发、并行、同步、异步、多线程的区别
- LeetCode 20 Valid Parentheses
- ROS探索总结(三)——ROS新手教程
- LeetCode 21 Merge Two Sorted Lists
- TCP首部分析及三次握手