Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
}
});

}
}
Posted
Comments
Richard MacCutchan 11-Sep-18 5:56am    
What exactly is your question?
Member 13980253 11-Sep-18 7:53am    
I need to split a text file in to 10 separate files by using multiple threads..
Richard MacCutchan 11-Sep-18 11:21am    
Yes, you have already said that. But you have not explained what the problem is in terms of what you cannot do, or what does not work etc. And before you start working on threads you need to be sure that that is the best solution to the problem.
Member 13980253 12-Sep-18 1:29am    
I am little bit confused just know i started coding and its my task to do with multiple threads.. Will you please suggest me some idea or i would be thankful if you help me in coding.Does using multiple threads is good idea to split a file.. i know using multiple threads can slow the execution. but I need to do with threads only :(
Member 13980253 12-Sep-18 1:44am    
I am unable to split the file with multiple threads..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900