Warm tip: This article is reproduced from serverfault.com, please click

Constructor that receives TXT files to read and store them

发布于 2020-11-28 01:14:36

Firstly i already have this code:

public class TextTest {
public static void main(String[] args) {    
    try {
        List<Text> v = new ArrayList<Text>();       
        v.add(new Text(args[0]));
        v.add(new Text(args[1]));
        v.add(new Text(args[2]));       
        System.out.println(v); 
        Collections.sort(v);        
        System.out.println(v);
    }
    catch ( ArrayIndexOutOfBoundsException e) {
        System.err.println("Enter three files for comparation");
    }
}   

And i need to create a class called Text with gonna contain the code who will open and read the TXT file. But, how i build the constructor to receive the directory with gonna contains the 3 TXT files, and at the same time, create the archives and read them? The files will be stored here:

        v.add(new Text(args[0]));
        v.add(new Text(args[1]));
        v.add(new Text(args[2]));
Questioner
Luciano Balestrin Correa
Viewed
0
Sash Sinha 2020-11-28 11:48:52

Say you have 3 text files text1.txt, text2.txt, and text3.txt with the following contents:

text1.txt:

A B C D E F G H I J 

text2.txt:

A B C D E F G H I J K L M N O

text3.txt:

A B C D E F G H I J K L

You could make Text implement Comparable<Text>:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Text implements Comparable<Text> {
    private final String fileName;
    private String fileData;
    private int wordCount;

    public Text(String filePath) {
        File file = new File(filePath);
        this.fileName = file.getName();
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
            StringBuilder sb = new StringBuilder();
            String line = bufferedReader.readLine();
            int wordCount = 0;
            while (line != null) {
                wordCount += line.split(" ").length;
                sb.append(line);
                sb.append(System.lineSeparator());
                line = bufferedReader.readLine();
            }
            this.fileData = sb.toString().strip();
            this.wordCount = wordCount;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getFileData() {
        return this.fileData;
    }

    @Override
    public String toString() {
        return String.format("%s [%d]", this.fileName, this.wordCount);
    }

    @Override
    public int compareTo(Text t) {
        if (this.wordCount == t.wordCount) {
            return this.fileData.compareTo(t.getFileData());
        }
        return this.wordCount - t.wordCount;
    }
}

Example Usage:

$ javac TestText.java Test.java
$ java TextText text1.txt text2.txt text3.txt
[text1.txt [10], text2.txt [15], text3.txt [12]]
[text1.txt [10], text3.txt [12], text2.txt [15]]