Java: I want to read a text file, split it based on column value into multiple files : public class FileSplitter2 {

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

        String filepath = "D:\\temp\\test.txt";

        BufferedReader reader = new BufferedReader(new FileReader(filepath));
        String strLine;
        boolean isFirst = true;
        String strGroupByColumnName = "city";
        int positionOgHeader = 0;
        FileWriter objFileWriter;
        Map<String, FileWriter> groupByMap = new HashMap<String, FileWriter>();
        while ((strLine = reader.readLine()) != null) {
            String[] splitted = strLine.split(",");
            if (isFirst) {
                isFirst = false;
                for (int i = 0; i < splitted.length; i++) {
                    if (splitted[i].equalsIgnoreCase(strGroupByColumnName)) {
                        positionOgHeader = i;
                        break;
                    }
                }
            }
            String strKey = splitted[positionOgHeader];
            if (!groupByMap.containsKey(strKey)) {
                groupByMap.put(strKey, new FileWriter("D:/TestExample/" + strKey + ".txt"));
            }
            FileWriter fileWriter = groupByMap.get(strKey);
            fileWriter.write(strLine);
        }
        for (Map.Entry<String,FileWriter> entry : groupByMap.entrySet()) {
        entry.getKey();
        }
 
    }
}
This is my code.. i am not getting proper result.. Someone please help me out..the file contains 10 columns and 5th column is city.. they are 10 different cities in a file.. i need to split each city in a separate file...
