package cun;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.awt.event.ActionEvent;
public class QQQ extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel lblNewLabel_1;
private static String path = "E:/";
private static String filenameTemp;
ArrayList<Double> k=new ArrayList<>();
File filename;
private JButton btnNewButton_1;
private JButton btnNewButton_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QQQ frame = new QQQ();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public QQQ() {
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(39, 59, 129, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(207, 59, 165, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblNewLabel = new JLabel("\u8F93\u51655\u4E2A\u6D6E\u70B9\u6570\u636E\uFF1A");
lblNewLabel.setBounds(39, 20, 112, 29);
contentPane.add(lblNewLabel);
lblNewLabel_1 = new JLabel("\u8F93\u5165\u6587\u4EF6\u5939\u76EE\u5F55");
lblNewLabel_1.setBounds(218, 27, 100, 15);
contentPane.add(lblNewLabel_1);
JButton btnNewButton = new JButton("\u5B58\u5230\u6587\u4EF6");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//窗口读入存入文件
String str=textField.getText();
String file_name=textField_1.getText();
ArrayList<Double> v=new ArrayList<>();
String[] strarray=str.split(" ");
for(int i=0;i<strarray.length;i++) {
v.add(Double.parseDouble(strarray[i]));
}
filenameTemp=path+file_name+".txt";
filename=new File(filenameTemp);
if(!filename.exists()) {
try {
filename.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
write(filename,v);
}
catch(IOException e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(207, 110, 97, 23);
contentPane.add(btnNewButton);
btnNewButton_1 = new JButton("\u6392\u5E8F");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
///从文件中读出后sort
String gettxt = null;
try {
gettxt = read(filenameTemp);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(gettxt);
String[] strarray2=gettxt.split(" ");
for(int i=0;i<strarray2.length;i++) {
k.add(Double.parseDouble(strarray2[i]));
}
///System.out.println("f");
for(int i=0;i<strarray2.length;i++) {
System.out.println(strarray2[i]);
}
Collections.sort(k,Collections.reverseOrder());
for(int i=0;i<k.size();i++) {
System.out.println(k.get(i));
}
}
});
btnNewButton_1.setBounds(207, 159, 97, 23);
contentPane.add(btnNewButton_1);
btnNewButton_2 = new JButton("\u8FFD\u52A0");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
///追加写入
write(filename,k);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_2.setBounds(207, 213, 97, 23);
contentPane.add(btnNewButton_2);
}
///写入文件
public static void write(File file, ArrayList<Double> a) throws IOException {
// OutputStreamWriter可以显示指定字符集,否则使用默认字符集
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
String content="";
for(int i = 0; i < a.size(); i++) {
content += "" + a.get(i) + " ";
}
osw.write(content);
osw.close();
}
///读取文件
public static String read(String filenameTemp2) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream(filenameTemp2), "UTF-8");
char[] chars = new char[1024];
StringBuilder sb = new StringBuilder();
int cnt = 0;
int length = 0;
while ((length = isr.read(chars)) != -1) {
sb.append(chars, 0, length);
cnt += length;
}
isr.close();
return sb.toString();
}
}