Skip to content

Instantly share code, notes, and snippets.

@danilomiranda
Created June 26, 2015 18:02
Show Gist options
  • Save danilomiranda/9a6674894cfaf1034530 to your computer and use it in GitHub Desktop.
Save danilomiranda/9a6674894cfaf1034530 to your computer and use it in GitHub Desktop.
Dividir arquivo CSV em arquivos menores
String charset = "Cp1252";
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("CAMINHO ARQUIVO ORIGEM"), charset));
String line="";
String destiny="CAMINHO ARQUIVO DESTINO";
int count = 1;
int fileSize = 0;
OutputStreamWriter fos = new OutputStreamWriter(new FileOutputStream(destiny+count+".csv"),charset);
while((line = bufferedReader.readLine()) != null) {
if(fileSize + line.getBytes().length > 50 * 1024 * 1024){
fos.flush();
fos.close();
count++;
fos = new OutputStreamWriter(new FileOutputStream(destiny+count+".csv"),charset);
fos.write(line+"\n");
fileSize = line.getBytes().length;
}else{
fos.write(line+"\n");
fileSize += line.getBytes().length;
}
}
fos.flush();
fos.close();
bufferedReader.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment