Java上级实验三

问题描述 

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

源代码

 

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;

public class FileStream {
        public static void  main(String[] args) {
            MyFrame s3 = new MyFrame();
            s3.setVisible(true);
        }
    }

class MyFrame extends JFrame {
  private JPanel contentPane;
  private JTextField text1;
  private JTextField text2;
  private JTextField text3;
  private JTextField text4;
  private JTextField text5;
  private JTextField text6;
  private String str;

  public MyFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(800, 400, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    setTitle("文件读写");

    JLabel label = new JLabel("请输入5个数");
    label.setBounds(27, 13, 99, 25);
    contentPane.add(label);

    text1 = new JTextField();
    text1.setBounds(28, 38, 38, 34);
    contentPane.add(text1);
    text1.setColumns(10);

    text2 = new JTextField();
    text2.setColumns(10);
    text2.setBounds(78, 38, 38, 34);
    contentPane.add(text2);

    text3 = new JTextField();
    text3.setColumns(10);
    text3.setBounds(127, 38, 38, 34);
    contentPane.add(text3);

    text4 = new JTextField();
    text4.setColumns(10);
    text4.setBounds(176, 38, 38, 34);
    contentPane.add(text4);

    text5 = new JTextField();
    text5.setColumns(10);
    text5.setBounds(225, 38, 38, 34);
    contentPane.add(text5);

    JLabel label_1 = new JLabel("请输入文件名");
    label_1.setBounds(27, 82, 94, 19);
    contentPane.add(label_1);

    text6 = new JTextField();
    text6.setBounds(27, 107, 153, 31);
    contentPane.add(text6);
    text6.setColumns(10);

    JButton button1 = new JButton("写入文件");
    button1.setBounds(25, 164, 128, 27);
    contentPane.add(button1);
    button1.addActionListener(
        (e) -> { // 添加监听器,单击写入文件的按钮后产生事件
          str = text6.getText();
//	      System.out.println(str);
          String s1 = Float.parseFloat(text1.getText()) + " ";
          String s2 = Float.parseFloat(text2.getText()) + " ";
          String s3 = Float.parseFloat(text3.getText()) + " ";
          String s4 = Float.parseFloat(text4.getText()) + " ";
          String s5 = Float.parseFloat(text5.getText()) + "";
          String str1=s1+s2+s3+s4+s5+"\n";
          System.out.print("输入的数字为:"+str1);
          FileOutputStream fos = null;
          DataOutputStream dos = null;
          try {
//            File outFile = new File(str);
//            fos=new FileOutputStream(outFile);
            fos = new FileOutputStream(str);
            dos = new DataOutputStream(fos);
            dos.writeBytes(str1);
            JOptionPane.showMessageDialog(null,"写入成功");
          } catch (FileNotFoundException e1) {
              e1.printStackTrace();
          } catch (IOException e2) {
              e2.printStackTrace();
          } finally {// 确保流的关闭
            if (dos != null) {
              try {
                dos.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
            if (fos != null) {
              try {
                fos.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
          }
        });



    JButton button2 = new JButton("排序后写入文件");
    button2.setBounds(161, 164, 137, 27);
    contentPane.add(button2);
    button2.addActionListener(
        (e) -> { // 添加监听器,单击排序后写入文件的按钮后产生事件
          float[] f = new float[5];
          FileInputStream fis = null;
          DataInputStream dis = null;
          try { // 从文件读
            fis = new FileInputStream(str);
            dis = new DataInputStream(fis);
            String str1 = dis.readLine(); // 读取数据
            System.out.println("读取的数据为:" + str1);
            String[] arr = str1.split(" "); // 将数据转化为string型数组
            for (int i = 0; i <= 4; i++) {
              f[i] = Float.parseFloat(arr[i]); // 将数组转化为float型数组
            }
            Arrays.sort(f); // 将数组排序
            //            for (int i = 0; i <= 4; i++) {
            //              System.out.println(f[i]);
            //            }
          } catch (FileNotFoundException e1) {
            e1.printStackTrace();
          } catch (IOException e2) {
            e2.printStackTrace();
          } finally {// 确保流的关闭
            if (dis != null) {
              try {
                dis.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
            if (fis != null) {
              try {
                fis.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
          }

          FileOutputStream fos = null;
          DataOutputStream dos = null;
          try { // 排序后追加写入文件
            fos = new FileOutputStream(str, true); // 加上true后表明是追加,不然就会覆盖原来数据
            dos = new DataOutputStream(fos);
            String str2 = f[4] + " " + f[3] + " " + f[2] + " " + f[1] + " " + f[0] + "\n";
            System.out.println("排序后数据为:"+str2);
            dos.writeBytes(str2);
            JOptionPane.showMessageDialog(null, "排序写入成功");
          } catch (FileNotFoundException e1) {
            e1.printStackTrace();
          } catch (IOException e2) {
            e2.printStackTrace();
          } finally { // 确保流的关闭
            if (dos != null) {
              try {
                dos.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
            if (fos != null) {
              try {
                fos.close();
              } catch (IOException ioException) {
                ioException.printStackTrace();
              }
            }
          }
        });
  }
}

结果展示 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值