java小实验

文章目录


实验

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

代码如下(示例):


import javax.management.StringValueExp;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;


public class frame extends JFrame {

    private JLabel l1;
    private JLabel l2;
    private JButton b1;
    private JButton b2;
    private JTextField t1;
    private JTextField t2;
    private String str1;
    private String str2;


    frame() {
        setTitle("I/O STREAM");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        l1 = new JLabel("输入文件名:");
        l2 = new JLabel("输入内容:");
        b1 = new JButton("写入文件");
        b2 = new JButton("排序内容");
        t1 = new JTextField(80);
        t2 = new JTextField(40);
        this.setBounds(400, 300, 450, 350);
        this.add(l1);
        this.add(l2);
        this.add(b1);
        this.add(b2);
        this.add(t1);
        this.add(t2);
        this.setLayout(null);
        l1.setBounds(100, 80, 80, 20);
        l2.setBounds(100, 120, 150, 20);
        t1.setBounds(200, 80, 150, 18);
        t2.setBounds(200, 120, 150, 18);
        b1.setBounds(80, 210, 100, 18);
        b2.setBounds(280, 210, 100, 18);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    str1 = t1.getText();
                    System.out.println(str1);
                    str2 = t2.getText();
                    System.out.println(str2);
                    File outfile = new File("D:" + File.separator + str1 + ".txt");
                    if (!outfile.exists()) {
                        try {
                            outfile.createNewFile();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    FileOutputStream fos = new FileOutputStream(outfile);
                    // DataOutputStream dos = new DataOutputStream(fos);
                    PrintStream ps = new PrintStream(fos);
                    ps.printf(str2);
                    ps.close();
                    JOptionPane.showMessageDialog(null, "数据已存入!!!");

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


            }
        });


        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//+ File.separator +str1
                try {
                    File infile = new File("D:" + File.separator + "ainio.txt");
                    BufferedReader rd = null;
                    String s1, s2 = "", s3 = "";
                    //StringBuffer s3 = new StringBuffer();
                    rd = new BufferedReader(new FileReader(infile));
                    while ((s1 = rd.readLine()) != null) {
                        s2 += s1;
                    }
                    rd.close();

                    String[] arr1 = s2.split(" ");
                    int n = arr1.length;
                    float[] f = new float[n];
                    for (int i = 0; i < n; i++) {
                        f[i] = Float.parseFloat(arr1[i]);
                        //System.out.print(f[i]);
                    }
                    Arrays.sort(f);
                    for (int i = 0; i < n; i++) {
                        String s = String.valueOf(f[i]);
                        s3 = s3 + s + ' ';
                        // s3.append(s);

                    }
                    System.out.println(s3);
                    File outfile = new File("D:" + File.separator + str1 + ".txt");
                    FileOutputStream fos = new FileOutputStream(outfile, true);
                    PrintStream p1 = new PrintStream(fos);
                    p1.println();
                    p1.printf(s3);
                    p1.close();
                    JOptionPane.showMessageDialog(null, "数据处理完成!!!");

                } catch (FileNotFoundException e6) {
                    e6.printStackTrace();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }


            }
        });
         public static void main(String[] args) {
     frame f1 =new frame();
     f1.setVisible(true);
    }

    }


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210426180053591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MjQ3NTQ5NQ==,size_16,color_FFFFFF,t_70)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值