Guest User

SortFile

a guest
Apr 13th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. package sortFile;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JTextField;
  10. import javax.swing.JLabel;
  11. import javax.swing.JButton;
  12. import java.awt.event.ActionListener;
  13. import java.io.BufferedReader;
  14. import java.io.BufferedWriter;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.io.PrintWriter;
  20. import java.awt.event.ActionEvent;
  21.  
  22. @SuppressWarnings({ "unused", "serial" })
  23. public class NewJFrame extends JFrame {
  24.  
  25.     private JPanel contentPane;
  26.     private JTextField textField;
  27.     private JTextField textField_1;
  28.     private JTextField textField_2;
  29.     private String path,path_temp1,path_temp2;
  30.     /**
  31.      * Launch the application.
  32.      */
  33.     public static void main(String[] args) {
  34.         EventQueue.invokeLater(new Runnable() {
  35.             public void run() {
  36.                 try {
  37.                     NewJFrame frame = new NewJFrame();
  38.                     frame.setVisible(true);
  39.                 } catch (Exception e) {
  40.                     e.printStackTrace();
  41.                 }
  42.             }
  43.         });
  44.     }
  45.  
  46.     /**
  47.      * Create the frame.
  48.      */
  49.     public NewJFrame() {
  50.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         setTitle("Natural Merge Sort");
  52.         setBounds(100, 100, 554, 300);
  53.         contentPane = new JPanel();
  54.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  55.         setContentPane(contentPane);
  56.         contentPane.setLayout(null);
  57.        
  58.         textField = new JTextField();
  59.         textField.addActionListener(new ActionListener() {
  60.             public void actionPerformed(ActionEvent arg0) {
  61.                 path = textField.getText();
  62.             }
  63.         });
  64.         textField.setBounds(322, 21, 151, 20);
  65.         contentPane.add(textField);
  66.         textField.setColumns(10);
  67.        
  68.         textField_1 = new JTextField();
  69.         textField_1.addActionListener(new ActionListener() {
  70.             public void actionPerformed(ActionEvent e) {
  71.                 path_temp1 = textField_1.getText();
  72.             }
  73.         });
  74.         textField_1.setBounds(322, 52, 151, 20);
  75.         contentPane.add(textField_1);
  76.         textField_1.setColumns(10);
  77.        
  78.         textField_2 = new JTextField();
  79.         textField_2.addActionListener(new ActionListener() {
  80.             public void actionPerformed(ActionEvent e) {
  81.                 path_temp2 = textField_2.getText();
  82.             }
  83.         });
  84.         textField_2.setBounds(322, 83, 151, 20);
  85.         contentPane.add(textField_2);
  86.         textField_2.setColumns(10);
  87.        
  88.         JLabel lblciekaDoPliku = new JLabel("Ścieżka do pliku źródłowego");
  89.         lblciekaDoPliku.setBounds(23, 24, 289, 14);
  90.         contentPane.add(lblciekaDoPliku);
  91.        
  92.         JLabel lblciekaDoPierwszego = new JLabel("\u015Acie\u017Cka do pierwszego pliku tymczasowego");
  93.         lblciekaDoPierwszego.setBounds(21, 55, 291, 14);
  94.         contentPane.add(lblciekaDoPierwszego);
  95.        
  96.         JLabel lblciekaDoDrugiego = new JLabel("\u015Acie\u017Cka do drugiego pliku tymczasowego");
  97.         lblciekaDoDrugiego.setBounds(23, 89, 289, 14);
  98.         contentPane.add(lblciekaDoDrugiego);
  99.        
  100.         JButton btnSortuj = new JButton("Sortuj");
  101.         btnSortuj.addActionListener(new ActionListener() {
  102.             public void actionPerformed(ActionEvent e) {
  103.                 try {
  104.                     naturalMerge(path,path_temp1,path_temp2);
  105.                 } catch (IOException e1) {
  106.                     // TODO Auto-generated catch block
  107.                     e1.printStackTrace();
  108.                 }
  109.             }
  110.  
  111.             private void naturalMerge(String path, String path_temp1, String path_temp2) throws IOException {
  112.                 // TODO Auto-generated method stub
  113.                 boolean isEmpty2 = false;
  114.                 while(!isEmpty2)
  115.                 {
  116.                     BufferedReader in = new BufferedReader(new FileReader(path));
  117.                     PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(path_temp1)));
  118.                     PrintWriter out2 = new PrintWriter(new BufferedWriter(new FileWriter(path_temp2)));
  119.                     //Rozdzielanie pliku
  120.                     String line = null;
  121.                     String prevline = null;
  122.                     isEmpty2 = true;
  123.                     boolean swapFiles = true;
  124.                     if((line = in.readLine()) != null)
  125.                     {
  126.                         out1.println(line);
  127.                         prevline = line;
  128.                     }
  129.                     while((line = in.readLine()) != null)
  130.                     {
  131.                         if(prevline.compareTo(line) > 0)
  132.                             swapFiles = !swapFiles;
  133.                         if(swapFiles)
  134.                             out1.println(line);
  135.                         else
  136.                         {
  137.                             out2.println(line);
  138.                             isEmpty2 = false;
  139.                         }
  140.                         prevline = line;
  141.                     }
  142.                     in.close();
  143.                     out1.close();
  144.                     out2.close();
  145.                     //Scalanie plików
  146.                     String tmp1,tmp2;
  147.                     String prevTmp1 = null;
  148.                     String prevTmp2 = null;
  149.                     boolean endOfRunTmp1,endOfRunTmp2;
  150.                     BufferedReader in1 = new BufferedReader(new FileReader(path_temp1));
  151.                     BufferedReader in2 = new BufferedReader(new FileReader(path_temp2));
  152.                     PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
  153.                     tmp1 = in1.readLine();
  154.                     tmp2 = in2.readLine();
  155.                     endOfRunTmp1 = (tmp1 == null);
  156.                     endOfRunTmp2 = (tmp2 == null);
  157.                     while(tmp1 != null || tmp2 != null)
  158.                     {
  159.                         while(!endOfRunTmp1 && !endOfRunTmp2)
  160.                             if(tmp2.compareTo(tmp1) > 0)
  161.                             {
  162.                                 out.println(tmp1);
  163.                                 prevTmp1 = tmp1;
  164.                                 tmp1 = in1.readLine();
  165.                                 if(tmp1 == null || prevTmp1.compareTo(tmp1) > 0)
  166.                                     endOfRunTmp1 = true;
  167.                             }
  168.                             else
  169.                             {
  170.                                 out.println(tmp2);
  171.                                 prevTmp2 = tmp2;
  172.                                 tmp2 = in2.readLine();
  173.                                 if(tmp2 == null || prevTmp2.compareTo(tmp2) > 0)
  174.                                     endOfRunTmp2 = true;
  175.                             }
  176.                         while(!endOfRunTmp1)
  177.                         {
  178.                             out.println(tmp1);
  179.                             prevTmp1 = tmp1;
  180.                             tmp1 = in1.readLine();
  181.                             if(tmp1 == null || prevTmp1.compareTo(tmp1) > 0)
  182.                                 endOfRunTmp1 = true;
  183.                         }
  184.                         while(!endOfRunTmp2)
  185.                         {
  186.                             out.println(tmp2);
  187.                             prevTmp2 = tmp2;
  188.                             tmp2 = in2.readLine();
  189.                             if(tmp2 == null || prevTmp2.compareTo(tmp2) > 0)
  190.                                 endOfRunTmp2 = true;
  191.                         }
  192.                         endOfRunTmp1 = (tmp1 == null);
  193.                         endOfRunTmp2 = (tmp2 == null);
  194.                         prevTmp1 = null;
  195.                         prevTmp2 = null;
  196.                     }
  197.                     in1.close();
  198.                     in2.close();
  199.                     out.close();
  200.                 }
  201.             }
  202.         });
  203.         btnSortuj.setBounds(239, 176, 89, 23);
  204.         contentPane.add(btnSortuj);
  205.     }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment