how to read a table from text file in java

Solutions on MaxInterview for how to read a table from text file in java by the best coders in the world

showing results for - "how to read a table from text file in java"
Lya
24 Sep 2018
1import java.io.File;
2import java.io.FileNotFoundException;
3
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.Map;
7import java.util.Scanner;
8
9public class BuildTableWithArrays {
10
11    private static final String separator = "\\s+"; // regex for parsing lines
12    private static final int rowWidth = 4;
13
14    public static void main(String[] args) {
15
16        Map<Integer, ArrayList<String>> columns = buildMapWithColumnArrayLists("animals.txt");
17        printMap(columns);  // for demo
18        // if you want actual arrays
19        Map<Integer, String[]> colArrays = buildMapWithColumnArrays(columns);
20
21    }
22
23    public static Map<Integer, ArrayList<String>> buildMapWithColumnArrayLists(
24        String fileName) {
25
26        ArrayList<String> col0 = new ArrayList<String>();
27        ArrayList<String> col1 = new ArrayList<String>();
28        ArrayList<String> col2 = new ArrayList<String>();
29        ArrayList<String> col3 = new ArrayList<String>();
30
31        Map<Integer, ArrayList<String>> columns = new HashMap<Integer, ArrayList<String>>();
32        columns.put(0, col0);
33        columns.put(1, col1);
34        columns.put(2, col2);
35        columns.put(3, col3);
36
37        File file = new File(fileName);
38        try {
39            Scanner input = new Scanner(file);
40            while (input.hasNextLine()) {
41                String[] line = input.nextLine().trim().replaceAll(separator, " ")
42                    .split(separator);
43                for (int i = 0; i < rowWidth; i++) {
44                    if (line[i] == null) {
45                        columns.get(Integer.valueOf(i)).add("null");
46                    } else {
47                        columns.get(Integer.valueOf(i)).add(line[i]);
48                    }
49                }
50            }
51            input.close();
52        } catch (FileNotFoundException x) {
53            System.out.println(x.getMessage());
54        }
55
56        return columns;
57    }
58
59    public static void printMap(Map<Integer, ArrayList<String>> columns) {
60
61        for (int i = 0; i < rowWidth; i++) {
62            System.out.println("col" + i + " #elements = "
63                + columns.get(Integer.valueOf(i)).size());
64            for (String s : columns.get(Integer.valueOf(i))) {
65                System.out.print(s + " ");
66            }
67            System.out.println("\n");
68        }
69    }
70
71    public static String[] convertArrayList2Array (ArrayList<String> arrayList) {
72
73        String[] array = new String[arrayList.size()];
74        array = arrayList.toArray(array);
75        return array;
76
77    }
78
79    public static Map<Integer, String[]> buildMapWithColumnArrays(Map<Integer, ArrayList<String>> columns) {
80
81        Map<Integer, String[]> cols = new HashMap<Integer, String[]>(); 
82
83        for (Map.Entry<Integer, ArrayList<String>> entry : columns.entrySet()) {
84            Integer key = entry.getKey();
85            ArrayList<String> value = entry.getValue();
86            String[] val = convertArrayList2Array(value);
87            cols.put(key,val);
88        }
89
90        return cols;
91
92    }
93
94}
95