Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sortFile;
- 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.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.awt.event.ActionEvent;
- @SuppressWarnings({ "unused", "serial" })
- public class NewJFrame extends JFrame {
- private JPanel contentPane;
- private JTextField textField;
- private JTextField textField_1;
- private JTextField textField_2;
- private String path,path_temp1,path_temp2;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- NewJFrame frame = new NewJFrame();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public NewJFrame() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("Natural Merge Sort");
- setBounds(100, 100, 554, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- textField = new JTextField();
- textField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- path = textField.getText();
- }
- });
- textField.setBounds(322, 21, 151, 20);
- contentPane.add(textField);
- textField.setColumns(10);
- textField_1 = new JTextField();
- textField_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- path_temp1 = textField_1.getText();
- }
- });
- textField_1.setBounds(322, 52, 151, 20);
- contentPane.add(textField_1);
- textField_1.setColumns(10);
- textField_2 = new JTextField();
- textField_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- path_temp2 = textField_2.getText();
- }
- });
- textField_2.setBounds(322, 83, 151, 20);
- contentPane.add(textField_2);
- textField_2.setColumns(10);
- JLabel lblciekaDoPliku = new JLabel("Ścieżka do pliku źródłowego");
- lblciekaDoPliku.setBounds(23, 24, 289, 14);
- contentPane.add(lblciekaDoPliku);
- JLabel lblciekaDoPierwszego = new JLabel("\u015Acie\u017Cka do pierwszego pliku tymczasowego");
- lblciekaDoPierwszego.setBounds(21, 55, 291, 14);
- contentPane.add(lblciekaDoPierwszego);
- JLabel lblciekaDoDrugiego = new JLabel("\u015Acie\u017Cka do drugiego pliku tymczasowego");
- lblciekaDoDrugiego.setBounds(23, 89, 289, 14);
- contentPane.add(lblciekaDoDrugiego);
- JButton btnSortuj = new JButton("Sortuj");
- btnSortuj.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- naturalMerge(path,path_temp1,path_temp2);
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- private void naturalMerge(String path, String path_temp1, String path_temp2) throws IOException {
- // TODO Auto-generated method stub
- boolean isEmpty2 = false;
- while(!isEmpty2)
- {
- BufferedReader in = new BufferedReader(new FileReader(path));
- PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(path_temp1)));
- PrintWriter out2 = new PrintWriter(new BufferedWriter(new FileWriter(path_temp2)));
- //Rozdzielanie pliku
- String line = null;
- String prevline = null;
- isEmpty2 = true;
- boolean swapFiles = true;
- if((line = in.readLine()) != null)
- {
- out1.println(line);
- prevline = line;
- }
- while((line = in.readLine()) != null)
- {
- if(prevline.compareTo(line) > 0)
- swapFiles = !swapFiles;
- if(swapFiles)
- out1.println(line);
- else
- {
- out2.println(line);
- isEmpty2 = false;
- }
- prevline = line;
- }
- in.close();
- out1.close();
- out2.close();
- //Scalanie plików
- String tmp1,tmp2;
- String prevTmp1 = null;
- String prevTmp2 = null;
- boolean endOfRunTmp1,endOfRunTmp2;
- BufferedReader in1 = new BufferedReader(new FileReader(path_temp1));
- BufferedReader in2 = new BufferedReader(new FileReader(path_temp2));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
- tmp1 = in1.readLine();
- tmp2 = in2.readLine();
- endOfRunTmp1 = (tmp1 == null);
- endOfRunTmp2 = (tmp2 == null);
- while(tmp1 != null || tmp2 != null)
- {
- while(!endOfRunTmp1 && !endOfRunTmp2)
- if(tmp2.compareTo(tmp1) > 0)
- {
- out.println(tmp1);
- prevTmp1 = tmp1;
- tmp1 = in1.readLine();
- if(tmp1 == null || prevTmp1.compareTo(tmp1) > 0)
- endOfRunTmp1 = true;
- }
- else
- {
- out.println(tmp2);
- prevTmp2 = tmp2;
- tmp2 = in2.readLine();
- if(tmp2 == null || prevTmp2.compareTo(tmp2) > 0)
- endOfRunTmp2 = true;
- }
- while(!endOfRunTmp1)
- {
- out.println(tmp1);
- prevTmp1 = tmp1;
- tmp1 = in1.readLine();
- if(tmp1 == null || prevTmp1.compareTo(tmp1) > 0)
- endOfRunTmp1 = true;
- }
- while(!endOfRunTmp2)
- {
- out.println(tmp2);
- prevTmp2 = tmp2;
- tmp2 = in2.readLine();
- if(tmp2 == null || prevTmp2.compareTo(tmp2) > 0)
- endOfRunTmp2 = true;
- }
- endOfRunTmp1 = (tmp1 == null);
- endOfRunTmp2 = (tmp2 == null);
- prevTmp1 = null;
- prevTmp2 = null;
- }
- in1.close();
- in2.close();
- out.close();
- }
- }
- });
- btnSortuj.setBounds(239, 176, 89, 23);
- contentPane.add(btnSortuj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment