Developer Desk
Developer Desk

Reputation: 2344

How to store textfile content in arrayList..?

I have text file as follow I want to store this in an ArrayList but problem is it does not have same key value pair i.e last 2 [Param] has param4=value4,param5=value5.

How to store this in arrayList or map..?

test.txt

    [Param]
    param1=value1
    param2=value2
    param3=value3
    [Param]
    param1=value1
    param2=value2
    param3=value3
    [Param]
    param1=value1
    param2=value2
    param3=value3
    [Param]
    param1=value1
    param2=value2
    param3=value3
    [Param]
    param1=value1
    param2=value2
    param3=value3
    param4=value4
   [Param]
    param1=value1
    param2=value2
    param3=value3
    param4=value4
    param5=value5

Java Code:

public class Task {

    static String Filepath = "C:\\Users\\Administrator\\Desktop\\TASK\\test.txt";
    static ArrayList<String> list1;
    static ArrayList<String> list2;
    static ArrayList<String> list3;
    static HashMap<Integer, ArrayList<String>> map;

    public static void main(String[] args) {
        // TODO code application logic here

        try {
            FileInputStream fstream = new FileInputStream(Filepath);
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine, user;
            int lineNumber = 0;

            list1 = new ArrayList<>();
            list2 = new ArrayList<>();
            list3 = new ArrayList<>();
            map = new HashMap<>();

            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                process(strLine);
                lineNumber++;
            }
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        map.put(1, list1);
        map.put(2, list2);
        map.put(3, list3);

        for (int i = 0; i < list1.size(); i++) {
            System.out.println("[Param]");
            System.out.println("param1=" + list1.get(i));
            System.out.println("param2=" + list2.get(i));
            System.out.println("param3=" + list3.get(i));
        }

    }

    public static void process(String str) {

        if (str.contains("param1=")) {
            String[] arr = str.split("param1=");
            ////System.out.println(arr[1].trim());
            list1.add(arr[1].trim());
        } else if (str.contains("param2=")) {
            String[] arr = str.split("param2=");
            //System.out.println(arr[1].trim());
            list2.add(arr[1].trim());
        } else if (str.contains("param3=")) {
            String[] arr = str.split("param3=");
            //System.out.println(arr[1].trim());
            list3.add(arr[1].trim());
        } 
    }     
}

Upvotes: 0

Views: 74

Answers (1)

Mario
Mario

Reputation: 1801

Your text file seems a Windows INI file. ¿How about using Apache Commons Configuration? Concretely, the INIConfiguration class.

Upvotes: 1

Related Questions