Question

I read a text file containing list of words with their tags and put them as an ArrayList in an a wrapping ArrayList (ArrayList).

[[1, that, that, that, DT, DT], [2, table, table, table, NN, NN]]

Now I want to write the in a text file in a same format as follows:

1    that    that     that    DT    DT  
2    table   table    table   NN    NN

each of the above rows is an ArrayList with 6 columns.

the following code return a file with Ԁ inside.

public void setPPOSOfWordInDevelopmentList(ArrayList<ArrayList> trainingList){
try{
    FileOutputStream streamFile = new FileOutputStream("developmentFile.txt");
    ObjectOutputStream streamFileWriter = new ObjectOutputStream(streamFile);
for(ArrayList word: developmentWordsList){
    String inputWord = (String)word.get(1);
    extractTag(inputWord,trainingList);
    String extractedPPOSofWord =(String)findMaxTag().get(1);
    word.set(5, extractedPPOSofWord);

}
streamFileWriter.close();
System.out.println(developmentWordsList);
}
catch(Exception e){
    System.out.println("Something went wrong, check the code");
}
}

this code is coupled with some others so it is not easy to change the format of objects returned by the functions.

Was it helpful?

Solution

If you want to write a simple text file, would be better if you use a BufferedWriter. For your content, you can format it in a StringBuffer or a StringBuilder if it is too long. Here in this post, I replied to a question related with the kind of formatting you're trying to make. But you should need to adapt it according to your format and the logic of using a wrapping array.

Export array values to csv file java

I think, the loop or "enhanced for" statement should be used as something like:

for (ArrayList<String> innerArray: wrapperArray) {
    for (String word : innerArray) {
        //Adapt to your required format using a StringBuilder
    }
}
//Here at the end save the content of your StringBuilder or StringBuffer using the BufferedWriter.

Hope you can get an idea on how to achieve this. Best regards :)

OTHER TIPS

What you want sounds eerily like a standard CSV file. This stackoverflow thread will set you straight on how to parse that sort of content. I would strongly recommend that you refactor along the lines of a CSV file instead of using the ObjectInput/OutputStreams. It'll be easier to maintain and you'll be able to use tools like Excel and OpenOffice Calc to view your files when debugging.

If you are certain to use custom format file you can use formatted printing and add padding accordingly. It's pretty easy:

for (ArrayList<String> list : trainingList) {
    writeToStream(
        String.format(
            %s, %-5s, %-5s, %-5s, 
            list.getAt(0),list.getAt(1),list.getAt(2),list.getAt(3)
        );
    }
}

This should work if your strings aren't longer than five characters. Just keep in mind that blank characters are bad demiliters and you will face indentation problems if you use other than monospaced fonts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top