package ru.agolovin;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * File sort class.
 *
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */
public class FileSort {

    private String lineSeparator = System.getProperty("lineSeparator");

    public void sort(File source, File distance) throws IOException {
        try {
            RandomAccessFile rafSource = new RandomAccessFile(source, "r");
            RandomAccessFile rafDistance = new RandomAccessFile(distance, "rw");

            String line = rafSource.readLine();
            if (line != null) {
                rafDistance.writeBytes(line);
            }
            while ((line = rafSource.readLine()) != null) {
                rafDistance.writeBytes(String.format("%s%s", lineSeparator, line));
            }
        }
        catch (IOException ioe) {
            System.err.println("ioe");

        }
    }
}

@@ -19,39 +19,8 @@
 n;%0A%0A
-import java.io.BufferedReader;%0A
 impo
@@ -224,16 +224,100 @@
 Sort %7B%0A%0A
+    private RandomAccessFile rafSource;%0A%0A    private RandomAccessFile rafDistance;%0A%0A
     priv
@@ -463,118 +463,1235 @@
     
-try %7B%0A            RandomAccessFile rafSource = new RandomAccessFile(source, %22r%22);%0A            RandomAccessFile
+String fileDistance = %22distance.txt%22;%0A        try %7B%0A            checkExistFile(source);%0A%0A            distance = new File(fileDistance);%0A            RandomAccessFile rafDistance = new RandomAccessFile(distance, %22rw%22);%0A%0A            copyToDistance(source, distance);%0A%0A%0A            File tempOne = File.createTempFile(%22tempOne%22, %22.tmp%22);%0A            File tempTwo = File.createTempFile(%22tempTwo%22, %22.tmp%22);%0A%0A            RandomAccessFile rafTempOne = new RandomAccessFile(tempOne, %22rw%22);%0A            RandomAccessFile rafTempTwo = new RandomAccessFile(tempTwo, %22rw%22);%0A%0A            boolean flag = true;%0A            do %7B%0A                if (splitSourceFile() == 1)%7B%0A                    flag = false;%0A                %7D%0A                merge();%0A            %7D while (flag);%0A%0A        %7D catch (IOException ioe) %7B%0A            System.err.println(%22ioe%22);%0A%0A        %7D%0A    %7D%0A%0A    void checkExistFile (File file) throws IOException%7B%0A        if (file.exists() && file.isFile()) %7B%0A            throw new IOException(%22source file invalid%22);%0A        %7D%0A    %7D%0A%0A    public int splitSourceFile(File source) %7B%0A%0A    %7D%0A%0A    private void copyToDistance(File source, File distance) throws IOException%7B%0A        rafSource = new RandomAccessFile(source, %22r%22);%0A       
  raf
@@ -1739,28 +1739,24 @@
  %22rw%22);%0A
-%0A   
         
  String 
@@ -1743,25 +1743,24 @@
 %22);%0A        
- 
 String line 
@@ -1779,28 +1779,24 @@
 readLine();%0A
-    
         if (
@@ -1815,36 +1815,32 @@
 ) %7B%0A            
-    
 rafDistance.writ
@@ -1853,36 +1853,32 @@
 (line);%0A        
-    
 %7D%0A            wh
@@ -1871,20 +1871,16 @@
         
- 
-   
 while ((
@@ -1911,36 +1911,32 @@
 e()) != null) %7B%0A
-    
             rafD
@@ -1999,36 +1999,32 @@
 line));%0A        
-    
 %7D%0A        %7D%0A    
@@ -2021,98 +2021,33 @@
     
-%7D%0A        catch (IOException ioe) %7B%0A            System.err.println(%22ioe%22
+rafSource.close(
 );%0A
-%0A 
     
-   %7D%0A    %7D
+%7D%0A%0A
 %0A%7D%0A

add pre task 3