Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to read a table from text file in java

import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BuildTableWithArrays {

    private static final String separator = "s+"; // regex for parsing lines
    private static final int rowWidth = 4;

    public static void main(String[] args) {

        Map<Integer, ArrayList<String>> columns = buildMapWithColumnArrayLists("animals.txt");
        printMap(columns);  // for demo
        // if you want actual arrays
        Map<Integer, String[]> colArrays = buildMapWithColumnArrays(columns);

    }

    public static Map<Integer, ArrayList<String>> buildMapWithColumnArrayLists(
        String fileName) {

        ArrayList<String> col0 = new ArrayList<String>();
        ArrayList<String> col1 = new ArrayList<String>();
        ArrayList<String> col2 = new ArrayList<String>();
        ArrayList<String> col3 = new ArrayList<String>();

        Map<Integer, ArrayList<String>> columns = new HashMap<Integer, ArrayList<String>>();
        columns.put(0, col0);
        columns.put(1, col1);
        columns.put(2, col2);
        columns.put(3, col3);

        File file = new File(fileName);
        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String[] line = input.nextLine().trim().replaceAll(separator, " ")
                    .split(separator);
                for (int i = 0; i < rowWidth; i++) {
                    if (line[i] == null) {
                        columns.get(Integer.valueOf(i)).add("null");
                    } else {
                        columns.get(Integer.valueOf(i)).add(line[i]);
                    }
                }
            }
            input.close();
        } catch (FileNotFoundException x) {
            System.out.println(x.getMessage());
        }

        return columns;
    }

    public static void printMap(Map<Integer, ArrayList<String>> columns) {

        for (int i = 0; i < rowWidth; i++) {
            System.out.println("col" + i + " #elements = "
                + columns.get(Integer.valueOf(i)).size());
            for (String s : columns.get(Integer.valueOf(i))) {
                System.out.print(s + " ");
            }
            System.out.println("
");
        }
    }

    public static String[] convertArrayList2Array (ArrayList<String> arrayList) {

        String[] array = new String[arrayList.size()];
        array = arrayList.toArray(array);
        return array;

    }

    public static Map<Integer, String[]> buildMapWithColumnArrays(Map<Integer, ArrayList<String>> columns) {

        Map<Integer, String[]> cols = new HashMap<Integer, String[]>(); 

        for (Map.Entry<Integer, ArrayList<String>> entry : columns.entrySet()) {
            Integer key = entry.getKey();
            ArrayList<String> value = entry.getValue();
            String[] val = convertArrayList2Array(value);
            cols.put(key,val);
        }

        return cols;

    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: convert pdf to word in java 
Java :: set gamemode of player spigot 
Java :: singleton implementation 
Java :: date and time java 
Java :: How to make a Java Main Menu Loop after using a case 
Java :: java foreach letter in word 
Java :: module java.base does not "opens java.io" to unnamed module 
Java :: mod 10e9+7 in java 
Java :: reverse arraylist java recursion 
Java :: types of methods in java 
Java :: how to get the memory location of an object in java 
Java :: java inner method 
Java :: spring boot api key authentication example 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: Compare two csv files using java 
Java :: difference between two sets java 
Java :: Value Change Listener to JTextField 
Java :: java linkedlist poll 
Java :: how to make window in java 
Java :: string compareto vs equals java 
Java :: java variable 
Java :: sort java array 
Java :: Java Creating an EnumMap 
Java :: java scanner next() 
Java :: parameterized constructor java 
Java :: java to c# converter 
Java :: get string size 
Java :: positive numbers in array 
Java :: stringbuilder example in java 
Java :: singleton 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =