第三次试验

//package zhoutong;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class FileProc extends JFrame implements ActionListener{
	public static void main(String args[]){
		FileProc Myfile = new FileProc();
		Myfile.setVisible(true);
	}
	JLabel jl1,jl2,jl3;
	String s ;
	public Container container = new Container();
	JTextField jt1,jt2,jt3;
	FileProc(){
		super("浮点数排序:");
		setLayout(new GridLayout(8,1));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(400,400);
		container = getContentPane();
		setSize(400,300);
		jl2 = new JLabel("请输入保存文件位置:");
		getContentPane().add(jl2);
		jt2 = new JTextField();
		getContentPane().add(jt2);	
		jl1 = new JLabel("请输入5个浮点数:");
		getContentPane().add(jl1);
		jt1 = new JTextField();
		jt1.addActionListener(this);
	
		getContentPane().add(jt1);
		Container container = getContentPane();
		jt3 = new JTextField("结果");
		jl3 = new JLabel("处理结果");
		getContentPane().add(jl3);
		getContentPane().add(jt3);
	}
	public void actionPerformed(ActionEvent e){
		try{
			s = jt1.getText();
			String ss= jt2.getText();
			File outFile = new File(ss);
			FileOutputStream fos = new FileOutputStream(outFile);
			File inFile = new File(ss);
			FileInputStream fin = new FileInputStream(inFile);
			int c=s.length();
			int i;
			for(i=0;i<c;i++){
				fos.write(s.charAt(i));
			}
			fos.close();
			Scanner scanner = new Scanner(fin);
			double a[] = new double[5];
			i=0;
			while(scanner.hasNextDouble()){
				i++;
				a[i]=scanner.nextDouble();
			}
			if(i!=6)
				throw new ArrayIndexOutOfBoundsException();
			scanner.close();
			Arrays.sort(a,0,5);
			File out = new File(ss);
			FileOutputStream fis = new FileOutputStream(out,true);
			PrintStream ps = new PrintStream(fis);
			for(i=0;i<5;i++)
				ps.printf(" %f",a[i]);
			ps.close();
			jt3.setText("输入成功,排序成功,到当前目录下查看");
		}catch(FileNotFoundException c){
			jt3.setText("error:file no found");
		}catch(IOException c){
			jt3.setText("error: I/O error");
		}catch(ArrayIndexOutOfBoundsException c){
			jt3.setText("error: ArrayIndexOutOfBoundsException");
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值