提取一个大的txt文件并将其转换为数组字符串

时间:2019-03-10 03:14:10

标签: java

我很难有效地拿起一本非常大的书并将其变成数组字符串。我不断收到java.lang.OutOfMemoryError。 您可以通过查看并了解如何解决此问题来提供帮助吗?我正在尝试在十秒钟内处理整本书。

public static void main(String args[]) throws IOException {

    File read = new File("TaleOfTwoCities.txt");
    Scanner in = new Scanner(read);

    ArrayList<String> temporary = new ArrayList<String>();

    while (in.hasNext()) {
        temporary.add(in.next());
    }

    String[] words1 = temporary.toArray(new String[temporary.size()]);


    //String words1[] = {"I", "Just", "want", "this,", "to,", "work", "already"};
    TextJustification awl = new TextJustification();
    System.out.println(awl.justify(words1, 60));
}

如您所见,我尝试实现的格式为{“ I”,“ Just”,“ want”,... ect。

我不太确定如何解决我的问题,但是任何帮助将不胜感激!谢谢!

编辑:用户已要求查看txt文件,因此我已将其上传到保管箱,可以在https://www.dropbox.com/s/5sy5zp4n3b6wgfz/TaleOfTwoCities.txt?dl=0看到它

编辑2:这是完整程序https://pastebin.com/WyzD8zPH

2 个答案:

答案 0 :(得分:0)

要从文本文件中读取单词,可以使用FileReader和BufferedReader。

try (FileReader fileReader = new FileReader(new File("this_file_name"));
BufferedReader bufferedReader = new BufferedReader(fileReader);) {
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        String[] charLine = line.split(" ");
        for (String string: charLine) {
            System.out.println(string);
        }
    }
} catch(IOException e) {
    // Block of code to handle errors
}

答案 1 :(得分:0)

使用Map而不是数组来避免Exception。但是您仍然需要优化代码。运行此代码将需要很长时间。

这是我的样品

public class TextJustification {

    private String getKey(int i, int j) {
        return String.valueOf(i) + "##" + String.valueOf(j);
    }

    public String justify(String words[], int width) {

        Map<String, Integer> cost = new HashMap<>();

        //next 2 for loop is used to calculate cost of putting words from
        //i to j in one line. If words don't fit in one line then we put
        //Integer.MAX_VALUE there.
        for (int i = 0; i < words.length; i++) {
            cost.put(getKey(i, i), width - words[i].length());
            for (int j = i + 1; j < words.length; j++) {
                cost.put(getKey(i, j), cost.get(getKey(i, j - 1)) - words[j].length() - 1);
            }
        }

        for (int i = 0; i < words.length; i++) {
            for (int j = i; j < words.length; j++) {
                if (cost.get(getKey(i, j)) < 0) {
                    cost.put(getKey(i, j), Integer.MAX_VALUE);
                } else {
                    cost.put(getKey(i, j), (int) Math.pow(cost.get(getKey(i, j)), 2));
                }
            }
        }

        //minCost from i to len is found by trying
        //j between i to len and checking which
        //one has min value
        int minCost[] = new int[words.length];
        int result[] = new int[words.length];
        for (int i = words.length - 1; i >= 0; i--) {
            Integer costValue = cost.get(getKey(i, words.length - 1));
            minCost[i] = costValue;
            result[i] = words.length;
            for (int j = words.length - 1; j > i; j--) {
                if (costValue == Integer.MAX_VALUE) {
                    continue;
                }
                if (minCost[i] > minCost[j] + costValue) {
                    minCost[i] = minCost[j] + costValue;
                    result[i] = j;
                }
            }
        }
        int i = 0;
        int j;

        System.out.println("Minimum cost is " + minCost[0]);
        System.out.println("\n");
        //finally put all words with new line added in
        //string buffer and print it.
        StringBuilder builder = new StringBuilder();
        do {
            j = result[i];
            for (int k = i; k < j; k++) {
                builder.append(words[k] + " ");
            }
            builder.append("\n");
            i = j;
        } while (j < words.length);

        return builder.toString();
    }

    public static void main(String args[]) throws IOException {

        File read = new File("TaleOfTwoCities.txt");
        Scanner in = new Scanner(read);

        ArrayList<String> temporary = new ArrayList<String>();

        while (in.hasNext()) {
            temporary.add(in.next());
        }

        String[] words1 = temporary.toArray(new String[temporary.size()]);

        //String words1[] = {"I", "Just", "want", "this,", "to,", "work", "already"};
        TextJustification awl = new TextJustification();
        System.out.println(awl.justify(words1, 60));
    }
}