For a project i am working on, I am trying to split a text file into multiple files based on column with multiple thread..
i have a text file with 10 columns and 10000000 rows..
i need to split text file based on column name i.e., city.
I have tried a program where i am successfully splitting a file based on column name..
But I need to write a program with multiple threads..
i am very new to java.. its my task
Please ....Please.. someone help me with this..
thanks in advance :)
What I have tried:
public class FileSplitter2 {
public static void main(String[] args) throws IOException {
String filepath = "D:\\sample\\file.txt";
BufferedReader reader = new BufferedReader(new FileReader(filepath));
String strLine;
boolean isFirst = true;
String strGroupByColumnName = "CITY";
int positionOfHeader = 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)) {
positionOfHeader = i;
break;
}
}
}
String strKey = splitted[positionOfHeader];
if(!groupByMap.containsKey(strKey)) {
groupByMap.put(strKey, new FileWriter("D:/TestExample/" + strKey + ".txt"));
}
FileWriter fileWriter = groupByMap.get(strKey);
fileWriter.write(strLine + "\n");
}
for (Map.Entry<String, FileWriter> entry : groupByMap.entrySet()) {
entry.getKey();
}
groupByMap.values().forEach(fileWriter -> {
try {
fileWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
});
}
}