I am trying to read a table from a csv text file and generate a table (List
of Hashmap
s) in java.
For which I'm reading each line of text file, constructing a Hashmap<String, String>
record out of the line and appending it to a ArrayList
at the end of each iteration.
I am expecting a single instance of each line from text file to appear only once in the List, but all getting is the last row from text file appearing n+1
times, n being the last row number.
Here's the code:
public static void main(String[] args) throws IOException {
FileReader filObj = null;
try {
filObj = new FileReader(new File(System.getProperty("user.home") + "\\Desktop\\testData.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(filObj);
List<String> headers = new ArrayList<String>();
List<HashMap<String, String>> myTable = new ArrayList<HashMap<String, String>>();
HashMap<String, String> myRecord = new HashMap<String, String>();
String line = null;
int ext = 0;
while ((line = br.readLine()) != null) {
//System.out.println(line);
if (ext == 0) {
headers = Arrays.asList(line.split(","));
} else {
int index = 0;
for (String each : line.split(",")) {
myRecord.put(headers.get(index), each);
index++;
}
System.out.println("myrecord:" + myRecord);
}
myTable.add(myRecord);
ext++;
System.out.println("My Table:" + myTable);
}
}
the testData.txt
file contents are as below
TransactionNumber,TransactionType,Amount,TransactionDate,TransactionRemarks
123456,Credit,4000,07/10/2021,Salary Credited
123333,Debit,7000,05/10/2021,Fuel
123446,Credit,3000,01/10/2021,Refund
and the console output is as below:
My Table:[{}]
myrecord:{TransactionType=Credit, TransactionNumber=123456, Amount=4000, TransactionRemarks=Salary Credited, TransactionDate=07/10/2021}
My Table:[{TransactionType=Credit, TransactionNumber=123456, Amount=4000, TransactionRemarks=Salary Credited, TransactionDate=07/10/2021}, {TransactionType=Credit, TransactionNumber=123456, Amount=4000, TransactionRemarks=Salary Credited, TransactionDate=07/10/2021}]
myrecord:{TransactionType=Debit, TransactionNumber=123333, Amount=7000, TransactionRemarks=Fuel, TransactionDate=05/10/2021}
My Table:[{TransactionType=Debit, TransactionNumber=123333, Amount=7000, TransactionRemarks=Fuel, TransactionDate=05/10/2021}, {TransactionType=Debit, TransactionNumber=123333, Amount=7000, TransactionRemarks=Fuel, TransactionDate=05/10/2021}, {TransactionType=Debit, TransactionNumber=123333, Amount=7000, TransactionRemarks=Fuel, TransactionDate=05/10/2021}]
myrecord:{TransactionType=Credit, TransactionNumber=123446, Amount=3000, TransactionRemarks=Refund, TransactionDate=01/10/2021}
My Table:[{TransactionType=Credit, TransactionNumber=123446, Amount=3000, TransactionRemarks=Refund, TransactionDate=01/10/2021}, {TransactionType=Credit, TransactionNumber=123446, Amount=3000, TransactionRemarks=Refund, TransactionDate=01/10/2021}, {TransactionType=Credit, TransactionNumber=123446, Amount=3000, TransactionRemarks=Refund, TransactionDate=01/10/2021}, {TransactionType=Credit, TransactionNumber=123446, Amount=3000, TransactionRemarks=Refund, TransactionDate=01/10/2021}]