Results 1 to 1 of 1
Thread: union of two file with array
- 10-29-2011, 11:25 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 1
- Rep Power
- 0
union of two file with array
Sorry for my bad language.
I am beginner of Java.
This is my approach:
I am trying to read two files and then get the union of them. I should use an array with size 100. (just one array allowed)
First, I read all records from file1, and write them to the output, a third file. For that purpose, I read 100 record at a time, and write them to the third file using iteration.
After that, like first file, this time I read second file as 100 records at a time, and write them to the array, memory[]. Then I find the common records, if the record which I read from File2 is not in File1, I write it to the output file. I do this until reader2.readLine() gets null and I re-open File1 in each iteration.
This is what I have done so far, almost done, bu it gives null pointer exception. Any help would be appreciated.
exception line: while(!line11.equals(memory[k]))
Java Code:import java.io.*; public class FileUnion { private static long startTime, endTime; public static void main(String[] args) throws IOException { System.out.println("PROCESSING..."); reset(); startTimer(); String[] memory = new String[100]; int memorySize = memory.length; File file1 = new File("stdlist1.txt"); BufferedReader reader1 = new BufferedReader(new FileReader(file1)); File file3 = new File("union.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter(file3)); String line1 = null; String line11 = null; while((line1 = reader1.readLine()) != null) { for (int i = 0; i < 100; ) { memory[i] = line1; i++; if(i < 100) { line1 = reader1.readLine(); } } for (int i = 0; i < 100; i++) { writer.write(memory[i]); writer.newLine(); } } reader1.close(); File file2 = new File("stdlist2.txt"); BufferedReader reader2 = new BufferedReader(new FileReader(file2)); String line2 = null; while((line2 = reader2.readLine()) != null) { for (int i = 0; i < 100; i++) { memory[i] = line1; i++; if(i < 100) { line2 = reader2.readLine(); } } for (int k = 0; k < 100; k++ ) { boolean found = false; File f1 = new File("stdlist1.txt"); BufferedReader buff1 = new BufferedReader(new FileReader(f1)); while(((line11 = buff1.readLine()) != null) && found == false) { while(!line11.equals(memory[k])) { line11 = buff1.readLine(); } if(line11.equals(memory[k])) { found = true; } else { writer.write(memory[k]); } } buff1.close(); } } reader2.close(); writer.close(); endTimer(); long time = duration(); System.out.println("PROCESS COMPLETED SUCCESSFULLY"); System.out.println("Duration: " + time + " ms"); } public static void startTimer() { startTime = System.currentTimeMillis(); } public static void endTimer() { endTime = System.currentTimeMillis(); } public static long duration() { return endTime - startTime; } public static void reset() { startTime = 0; endTime = 0; } }Last edited by pbrockway2; 10-30-2011 at 02:31 AM. Reason: code tags added
Similar Threads
-
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
sqljdbc driver UNION querys
By sashaxiv in forum JDBCReplies: 9Last Post: 02-24-2011, 12:37 PM -
Array, I/O with a .txt file problems
By basketball8533 in forum New To JavaReplies: 4Last Post: 10-28-2010, 09:49 AM -
union in linked list
By IT student in forum New To JavaReplies: 4Last Post: 01-02-2010, 11:34 AM -
Printing the union and instersection of two sets
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks