user2163684
user2163684

Reputation: 31

A Good Method to read files in Java

I Really would appreciate it if someone can help me with this. I am trying to do external sorting and I am stuck on the part of merging. I get how I should merge it just not sure what function to use.

Right now I am trying to read in the first words of multiple small text files and store them in a string array of the size of the amount of files. So basically I will have a string array of the first word of each file. Then I determine which one is the smallest alphabetically wise and write that to a new file, after that I would read the next word of the file of that smallest word. This word would be placed in the position of the smallest word that got outputted in string array and compare it to the rest of the first word from the other file. This will keep repeating till all words are sorted.

The main problem I am running into is the fact that I was using scanner and after the first run of comparing it cant switch the smallest word with the next word in the file because scanner don't keep a point of what it has read. I know readline do but since my files are all words separated by only a white space I can't use readline. Can someone please guide me to a sufficient reading function that can't help me solve this problem.

  for (int i = 0; i<B;i++)
  {
  try
  {
    BufferedReader ins = new BufferedReader(new FileReader(Run-"+ i + ".txt"));
    Scanner scanner2 = new Scanner(ins);
    temp3[i] = scanner2.next();

                System.out.println(temp3[i]);
            }
            catch(IOException e)
            {   
            }
        }
        for(int i=0;i<N;i++)
        {
            String smallest = temp3[0];
            int smallestfile = 0;
            for(j=0;j<B;j++)
            {
                int comparisonResult = smallest.compareTo(temp3[j]);
                if(comparisonResult>0)
                {
                smallest = temp3[j];
                smallestfile = j;
                }
            }
            BufferedReader ins = new BufferedReader(new FileReader("C:/Run-"+ smallestfile + ".txt"));
            Scanner scanner2 = new Scanner(ins);
            if(scanner2.hasNext())
            {
                temp3[smallestfile]=scanner2.next();
            }
        }
}
catch(Exception e)
{
}

Upvotes: 3

Views: 306

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74058

I'm not sure, if I understood you right, but a Scanner does keep the position in a file. You need just as many of them as there are files

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class so {
    // returns the index of the smallest word
    // returns -1 if there are no more words
    private static int smallest(String[] words) {
        int min = -1;
        for (int i = 0; i < words.length; ++i)
            if (words[i] != null) {
                if (min == -1 || words[i].compareTo(words[min]) < 0)
                    min = i;
            }

        return min;
    }

    public static void main(String[] args) throws FileNotFoundException {
        // open all files
        Scanner[] files = new Scanner[args.length];
        for (int i = 0; i < args.length; ++i) {
            File f = new File(args[i]);
            files[i] = new Scanner(f);
        }

        // initialize first words
        String[] first = new String[args.length];
        for (int i = 0; i < args.length; ++i)
            first[i] = files[i].next();

        // compare words and read following words from scanners
        int min = smallest(first);
        while (min >= 0) {
            System.out.println(first[min]);
            if (files[min].hasNext()) {
                first[min] = files[min].next();
            } else {
                first[min] = null;
                files[min].close();
                files[min] = null;
            }

            min = smallest(first);
        }
    }
}

Tested with

a.txt: a d g j
b.txt: b e h k m
c.txt: c f i

Update:

In your example, you open and close the file inside the outer for loop. When you reopen a file the next time, it starts at the beginning of the file, of course.

To prevent this, you must keep the file open and move the scanner2 variable and its initialization in front of the outer for loop. You also need multiple Scanner variables, i.e. an array, to keep multiple files open simultaneously.

Upvotes: 0

hamilton.lima
hamilton.lima

Reputation: 1920

If the files are small enough read the entire file to memory, and use String.split() to separate the strings in arrays and do your magic.

If the the files are bigger, keep then open and read each byte until you find and space, then do it for all the files, compare the strings, do your magic and repeat until all the files reach the end.

EDIT :

  • how to read the files with BufferedReader
  • how to split the lines with String.split()

String line = readeOneLineFromTheCurrentFile(); String[] words = line.split(" ");

Upvotes: 1

user949300
user949300

Reputation: 15729

As for temporarily sorting/storing the words, use a PriorityQueue (not an array). Sorry, I'm too busy watching baseball to add more.

Upvotes: 0

Related Questions