Results 1 to 1 of 1
  1. #1
    onurmimar is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default 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 03:31 AM. Reason: code tags added

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  2. sqljdbc driver UNION querys
    By sashaxiv in forum JDBC
    Replies: 9
    Last Post: 02-24-2011, 01:37 PM
  3. Array, I/O with a .txt file problems
    By basketball8533 in forum New To Java
    Replies: 4
    Last Post: 10-28-2010, 10:49 AM
  4. union in linked list
    By IT student in forum New To Java
    Replies: 4
    Last Post: 01-02-2010, 12:34 PM
  5. Printing the union and instersection of two sets
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •