1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
// this.f est le fichier source
// this.splitSize est la taille voulue pour les parties obtenues...
public File[] split() {
int nbp = ((int) (this.size() / this.splitSize))+1;
FileReader fr = null;
try {fr = new FileReader(this.f);}
catch (FileNotFoundException e) {e.printStackTrace();}
File[] parts = new File[nbp];
for (int i=0;i<nbp;i++)
try {parts[i] = File.createTempFile("part", null);}
catch (IOException e) {e.printStackTrace();}
FileWriter[] fw = new FileWriter[nbp];
for (int i=0;i<nbp;i++)
try {fw[i] = new FileWriter(parts[i]);}
catch (IOException e) {e.printStackTrace();}
try {
int x = 0;
while (fr.ready()) {
fw[x].write(fr.read());
if (parts[x].length() > this.splitSize) {
x++;
}
}
fr.close();
for (int i=0;i<nbp;i++)
fw[i].close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return parts;
} |
Partager