preciso fazer uma aplicacao para dividir arquivos .txt em tantas linhas! Eu consegui fazer de um jeito, mais parece que fiz uma gambiarra ! vou postar para vcs darem uma olhada! o Codigo esta funcionando perfeito! So queria alguma dica de voces!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
public class Split {
private static String nomeArquivo;
private static String email;
private static String nomeArquivoDividido = "lista";
public static void split(int numLinhas) throws IOException {
FileReader ler = null;
BufferedReader origem = null;
int a = 1;
int count = 0;
String host;
String[] lista = new String[numLinhas+2];
try {
ler = new FileReader(new File(nomeArquivo));
origem = new BufferedReader(ler);
lista[0] = email;
for(host = origem.readLine(); host != null; host = origem.readLine()) {
lista[a] = host;
if (a == numLinhas) { // ARRAY CHEIO, SALVA A LISTA
lista[numLinhas+1] = email; // ADICIONA NA ULTIMA LINHA
String nome = nomeArquivoDividido+count+".txt";
PrintWriter salva = new PrintWriter(new FileWriter(nome), true);
for (int c = 0;c <= numLinhas+1;c++)
salva.println(lista[c]);
salva.close();
count++;
Arrays.fill(lista, null); // LIMPA ARRAY
a = 0; // ZERA A CONTAGEM DO ARRAY
lista[a] = email; //ADICIONA NA PRIMEIRA LINHA
}
a++;
}
if (host == null) { // AQUI SALVA O RESTANTE DO ARQUIVO QUE NAO CHEGA A LOTAR O ARRAY
String nome = nomeArquivoDividido+count+".txt";
PrintWriter salva = new PrintWriter(new FileWriter(nome), true);
for (int c = 0;c <= numLinhas+1 && lista[c] != null;c++)
salva.println(lista[c]);
salva.println(email);
salva.close();
}
} finally {
//if (origem != null)
//origem.close();
}
}
public static void main(String[] args) throws IOException {
nomeArquivo = args[0];
int linhas = Integer.parseInt(args[1]);
email = args[2];
split(linhas);
}
}