File(文件特征與管理):用于文件或者目錄的描述信息,例如生成新目錄,修改文件名,刪除文件,判斷文件所在路徑等。InputStream(二進制格式操作):抽象類,基于字節(jié)的輸入操作,是所有輸入流的父類。定義了所有輸入流都具有的共同特征。OutputStream(二進制格式操作):抽象類?;谧止?jié)的輸出操作。是所有輸出流的父類。定義了所有輸出流都具有的共同特征。FileOutputStream(文件輸出流,用于將數(shù)據(jù)寫出到文件。)FileInputStream(文件輸入流,從文件中讀取數(shù)據(jù)。)
package shiyan3;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;import javax.swing.*;
import java.io.*;
import java.util.Arrays;
public class shiyan3 { public static void main(String[] args) {shiyan3Frame s3 = new shiyan3Frame();s3.setVisible(true);}
}class shiyan3Frame extends JFrame{private JLabel lbl1;private JLabel lbl2;private JTextField text1;private JTextField text2;private JTextField text3;private JTextField text4;private JTextField text5;private JTextField Text;private JButton btn1;private JButton btn2;private String str;shiyan3Frame(){setTitle("實驗3");setSize(600, 400);this.setLayout(new GridLayout(10,1));text1 = new JTextField();text2 = new JTextField();text3 = new JTextField();text4 = new JTextField();text5 = new JTextField();Text = new JTextField();lbl1 = new JLabel("輸入文件名:");lbl2 = new JLabel("輸入數(shù)字:");btn1 = new JButton("打開文件");btn2 = new JButton("發(fā)送數(shù)字");btn1.addActionListener(new ActionListener(){ //添加監(jiān)聽器,單擊打開文件的按鈕后產(chǎn)生事件public void actionPerformed(ActionEvent e){str = Text.getText();System.out.println(str);}});btn2.addActionListener(new ActionListener(){ //添加監(jiān)聽器,單擊打開文件的按鈕后產(chǎn)生事件public void actionPerformed(ActionEvent e){String s1 = text1.getText()+" "; String s2 = text2.getText()+" ";String s3 = text3.getText()+" ";String s4 = text4.getText()+" ";String s5 = text5.getText();float[] f = new float[5];try {
// System.out.println(str);System.out.println(s1+s2+s3+s4+s5);File outFile = new File(str);FileOutputStream fos = new FileOutputStream(outFile);DataOutputStream dos = new DataOutputStream(fos);dos.writeBytes(s1);dos.writeBytes(s2);dos.writeBytes(s3);dos.writeBytes(s4);dos.writeBytes(s5);dos.writeBytes("\n");dos.close();fos.close();File inFile = new File(str);FileInputStream fis = new FileInputStream(inFile);DataInputStream dis = new DataInputStream(fis);String str1,Str="";while((str1 = dis.readLine())!=null) {Str += str1;}String[] arr = Str.split(" ");dis.close();fis.close();for (int i = 0; i <= 4; i++) {f[i] = Float.parseFloat(arr[i]);}Arrays.sort(f);s1 = f[4]+" ";s2 = f[3]+" ";s3 = f[2]+" ";s4 = f[1]+" ";s5 = f[0]+"";outFile = new File(str);fos = new FileOutputStream(outFile,true);//加上true后表明是追加,不然就一直會覆蓋dos = new DataOutputStream(fos);dos.writeBytes(s1);dos.writeBytes(s2);dos.writeBytes(s3);dos.writeBytes(s4);dos.writeBytes(s5);dos.close();fos.close();}catch(FileNotFoundException e1) {System.out.println("FileStream"+e1);}catch(IOException e2) {System.err.println("FileStream"+e2);}}});add(lbl1);add(Text);add(btn1);add(lbl2);add(text1);add(text2);add(text3);add(text4);add(text5);add(btn2);}
}
總結(jié)
以上是生活随笔為你收集整理的编写图形界面程序,接受用户输入的5个浮点数据和一个文件目录名,将这五个数据保存在该文件中,再从文件中读取出来并且进行从大到小排序,然后再一次追加保存在该文件中。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。