Java实验三报告IO流

实验目的:

了解Java IO,掌握IO处理的设计方法。重点掌握Java IO主要的类如下:
1. File(文件特征与管理):用于文件或者目录的描述信息,例如生成新目录,修改文件名,删除文件,判断文件所在路径等。
2. InputStream(二进制格式操作):抽象类,基于字节的输入操作,是所有输入流的父类。定义了所有输入流都具有的共同特征。
3. OutputStream(二进制格式操作):抽象类。基于字节的输出操作。是所有输出流的父类。定义了所有输出流都具有的共同特征。
4.FileOutputStream(文件输出流,用于将数据写出到文件。)
5. FileInputStream(文件输入流,从文件中读取数据。)

实验内容:

编写图形界面程序,接受用户输入的5个浮点数据和一个文件目录名,将这五个数据保存在该文件中,再从文件中读取出来并且进行从大到小排序,然后再一次追加保存在该文件中。

package com;
import java.awt.BorderLayout;
import java.util.Arrays;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.awt.event.ActionEvent;

public class Test02 extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private final JTextField textField_1 = new JTextField();
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Test02 frame = new Test02();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public static boolean isNumber(String str) {
        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }
    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    public int count = 0;
    private JButton btnNewButton_2;
    public Test02() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        textField = new JTextField();
        textField.setBounds(22, 24, 66, 21);
        contentPane.add(textField);
        textField.setColumns(10);
        textField_1.setBounds(97, 24, 66, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);
        textField_2 = new JTextField();
        textField_2.setBounds(173, 24, 66, 21);
        contentPane.add(textField_2);
        textField_2.setColumns(10);
        textField_3 = new JTextField();
        textField_3.setBounds(257, 24, 66, 21);
        contentPane.add(textField_3);
        textField_3.setColumns(10);
        textField_4 = new JTextField();
        textField_4.setBounds(345, 24, 66, 21);
        contentPane.add(textField_4);
        textField_4.setColumns(10);
        final double[] arr1 = new double[5];

        JButton btnNewButton = new JButton("\u8F93\u5165");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s1 = textField.getText();
                String s3 = textField_1.getText();
                String s4 = textField_2.getText();
                String s5 = textField_3.getText();
                String s6 = textField_4.getText();
                if (isNumber(s1) == true) {
                    arr1[0] = Double.parseDouble(s1);
                    textField.setText("");
                } else
                    textField.setText("");
                if (isNumber(s3) == true) {
                    arr1[1] = Double.parseDouble(s3);
                    textField_1.setText("");
                } else
                    textField_1.setText("");
                if (isNumber(s4) == true) {
                    arr1[2] = Double.parseDouble(s4);
                    textField_2.setText("");
                } else
                    textField_2.setText("");
                if (isNumber(s5) == true) {
                    arr1[3] = Double.parseDouble(s5);
                    textField_3.setText("");
                } else
                    textField_3.setText("");
                if (isNumber(s6) == true) {
                    arr1[4] = Double.parseDouble(s6);
                    textField_4.setText("");
                } else
                    textField_4.setText("");
                //五次之后按钮失效
            }
        });
        btnNewButton.setBounds(142, 55, 97, 23);
        contentPane.add(btnNewButton);
        textField_5 = new JTextField();
        textField_5.setBounds(22, 101, 107, 21);
        contentPane.add(textField_5);
        textField_5.setColumns(10);

        JButton btnNewButton_1 = new JButton("\u4FDD\u5B58\u6587\u4EF6");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    String s2 = textField_5.getText();
                    //label_2.setText(s2);
                    File outFile = new File(s2);
                    FileWriter fos = new FileWriter(outFile);
                    for (int c = 0; c < arr1.length; c++) {
                        fos.write(String.valueOf(arr1[c]) + "\r\n");
                    }
                    fos.close();
                } catch (Exception e1) {
                }
            }
        });
        btnNewButton_1.setBounds(172, 101, 97, 23);
        contentPane.add(btnNewButton_1);
        btnNewButton_2 = new JButton("\u6392\u5E8F");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int n = 0;
                    String c = null;
                    FileReader fis = new FileReader("D:\\1.txt");
                    BufferedReader br = new BufferedReader(fis);
                    double[] arr2 = new double[10];
                    while ((c = br.readLine()) != null) {

                        arr2[n++] = Double.parseDouble(c);

                    }
                    Arrays.sort(arr2, 0, 5);
                    FileWriter fos = new FileWriter("D:\\1.txt");
                    for (int i = 0; i < 5; i++) {
                        fos.write(String.valueOf(arr1[i]) + "\r\n");
                    }
                    for (int d = 4; d >= 0; d--) {
                        fos.write(String.valueOf(arr2[d]) + "\r\n");
                    }
                    fos.close();
                    br.close();
                } catch (FileNotFoundException e1) {

                    e1.printStackTrace();
                } catch (NumberFormatException e1) {

                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        btnNewButton_2.setBounds(122, 168, 97, 23);
        contentPane.add(btnNewButton_2);
    }
}

实验结果与体会:
将数据直接输入文件中,文件成功获取到数据
在这里插入图片描述
输入文件名,点击保存文件,成功保存文件
在这里插入图片描述
点击排序,成功将文件内的数据进行排序
在这里插入图片描述
心得和体会:
1.主要掌握了文件的读取,写入操作
2.实践了java中IO流的操作
3.进一步深化了图形界面的使用
4.加强了对于排序算法的实际运用

对于实验中出现的文件流基本错误有了基本的认识,初步掌握了javaIO流的基本操作,对字节流,字符流,文件流有了更加深刻的认识。锻炼了自身的排查错误的能力,加强了实际操作的编程能力,对标准流,对象流,过滤流等抽象概念也有了更进一步的认识和理解。

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值