How to serialise an array of 2 lists in a binary file?

41 views Asked by At

I am creating a Java program that reads two text files, selects 10 words at random from each file and stores them in an array of two string lists. I have created the following code so far, however this only reads the words, it doesn't store them. I also need to serialize the array of 2 lists in a binary file. How can this be done? Some help with this would be greatly appreciated!

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import static java.nio.file.StandardOpenOption.CREATE;

public class RandomWordGenerator {
    public static void main(String[] args) throws IOException {
        Path outputFile = Paths.get("output.txt");
        ArrayList<String> randomWords1 = randomWordsFromFile("input1.txt", 10);
        ArrayList<String> randomWords2 = randomWordsFromFile("input2.txt", 10);
        OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(outputFile, CREATE));
        System.out.println(randomWords1);
        System.out.println(randomWords2);
        outputStream.flush();
        for (int i = 0; i < randomWords1.size(); i++) {
            outputStream.write(randomWords1.get(i).getBytes());
        }
        for (int i = 0; i < randomWords2.size(); i++) {
            outputStream.write(randomWords2.get(i).getBytes());
        }
        outputStream.close();
    }

    private static ArrayList<String> randomWordsFromFile(String fileName, int count) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(fileName));
        ArrayList<String> words = new ArrayList<>();
        while (scanner.hasNext()) {
            words.add(scanner.next());
        }
        return randomFromWords(words, count);
    }

    static private ArrayList<String> randomFromWords(ArrayList<String> words, int count) {
        ArrayList<String> randomWords = new ArrayList<>();
        for (int i = 0; i < count; ) {
            int random = new Random().nextInt(words.size());
            if (randomWords.add(words.get(random))) {
                i++;
            }
        }
        return randomWords;
    }
}
1

There are 1 answers

1
rossum On

Your code should read two files, select ten random words from each file and store the ten words from each file into two lists. Does it do this correctly? If it does not then fix all the errors before proceeding further. You can ask here with specific errors you cannot fix. Be sure to explain what your code is meant to do, and the error it makes to prevent it doing that.

Once everything is working correctly, go on to the next stage. Is the binary Serialization part of your requirement? If not, then I would use a simple CSV file in UTF-8, which is also a binary file (as are all files at heart). If actual Serialization is required, then read the Javadocs and tutorials on Serialization. Write some code and come back here if you cannot get it to work.