Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

java split csv file into multiple csv files

Hi

I have a csv file with 4003 lines and the file name is "testnow.csv"
I want to split this csv file into 41 csv files, I mean each csv file cannot have more than 100 lines
and the new csv file name should be somthing like this

testnow_1.csv
testnow_2.csv
testnow_3.csv
.....

testnow_41.csv

Thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What's your OS?
Avatar of shragi

ASKER

Windows
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shragi

ASKER

@FishMOnger
small problem with your script, in order to use your script the files should be readily created before hand.
But is it possible to create the file along with the other logic.


Thanks
You said you have a csv file with 4003 lines
But if you need to create it, that can be done if you have the logic to generate the contents.
>>in order to use your script the files should be readily created before hand.

That's not the case. Did you try it?
@CEHJ
The code I gave does require the input csv file to already exist but not the output file.

I just noticed that I have a typo in the code which will cause it to fail.  I opened the output file in read mode, which certainly is not what is wanted.

Change:
open(my $csv_out, '<', "testnow_$num.csv")

Open in new window


To:
open(my $csv_out, '>', "testnow_$num.csv")

Open in new window