Problem:
Design and implement a merging algorithm that reads the data sets from two files of known length.
Solution:
package array.programs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeTwoFiles {
File file1;
File file2;
File resultFile;
List<String> firstFileData = new ArrayList<String>();
List<String> secondFileData = new ArrayList<String>();
public static void main(String[] args) throws IOException {
MergeTwoFiles mergeTwoFiles = new MergeTwoFiles();
mergeTwoFiles.mergeFiles();
}
public void mergeFiles() throws IOException{
file1 = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\file1.txt");
file2 = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\file2.txt");
resultFile = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\resultFile.txt");
FileReader fileReader = new FileReader(file1);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String data;
while ((data = bufferedReader.readLine()) != null) {
firstFileData.add(data);
}
fileReader = new FileReader(file2);
bufferedReader = new BufferedReader(fileReader);
while ((data = bufferedReader.readLine()) != null) {
secondFileData.add(data);
}
FileWriter fileWriter = new FileWriter(resultFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(String str: firstFileData){
bufferedWriter.write(str);
bufferedWriter.write("\n");
}
for(String str: secondFileData){
bufferedWriter.write(str);
bufferedWriter.write("\n");
}
bufferedReader.close();
bufferedWriter.close();
}
}
Design and implement a merging algorithm that reads the data sets from two files of known length.
Solution:
package array.programs;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MergeTwoFiles {
File file1;
File file2;
File resultFile;
List<String> firstFileData = new ArrayList<String>();
List<String> secondFileData = new ArrayList<String>();
public static void main(String[] args) throws IOException {
MergeTwoFiles mergeTwoFiles = new MergeTwoFiles();
mergeTwoFiles.mergeFiles();
}
public void mergeFiles() throws IOException{
file1 = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\file1.txt");
file2 = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\file2.txt");
resultFile = new File("D:\\hemanth\\DS&Al\\Programs\\src\\array\\programs\\resultFile.txt");
FileReader fileReader = new FileReader(file1);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String data;
while ((data = bufferedReader.readLine()) != null) {
firstFileData.add(data);
}
fileReader = new FileReader(file2);
bufferedReader = new BufferedReader(fileReader);
while ((data = bufferedReader.readLine()) != null) {
secondFileData.add(data);
}
FileWriter fileWriter = new FileWriter(resultFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(String str: firstFileData){
bufferedWriter.write(str);
bufferedWriter.write("\n");
}
for(String str: secondFileData){
bufferedWriter.write(str);
bufferedWriter.write("\n");
}
bufferedReader.close();
bufferedWriter.close();
}
}
No comments:
Post a Comment