0
我的標題足夠具體:我試圖將一個txt文件拆分爲多個具有相同大小的文件。 我沒做到這一點使用此功能:將文本文件分割成大小相同的文件,而不會在JAVA中打破文字
public static int fileSplitting(String fichier, String dossSortie, int nbMachines) throws FileNotFoundException, IOException{
int i=1;
File f = new File(fichier);
//FileReader fr = new FileReader(f);
//BufferedReader br = new BufferedReader(fr);
int sizeOfFiles = (int) (f.length()/(nbMachines));
System.out.print(sizeOfFiles);
byte[] buffer = new byte[sizeOfFiles];
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f))){
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
File newFile = new File(dossSortie+"S"+i);
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, tmp);//tmp is chunk size
}
i++;
}
}
return i;
}
的事情是,這個功能削減的話,而我需要保持每一個字。 例如,如果我有一個txt文件「我住在阿姆斯特丹」,該功能將會像這樣拆分它:「我住在Ams」,「terdam」。 我想這樣的事情:「我住在」,「阿姆斯特丹」。
謝謝大家!
如果文件應該具有完全相同的大小,就像尋找最大公約數的問題一樣:https://en.wikipedia.org/wiki/Greatest_common_divisor但是您需要爲所有長度的文字找到它 –
在「字節」的事情變得更加棘手。在'String'對象中讀取你的文件。你可以比'byte'輕鬆玩'String'。 –
如果你住在羅馬呢? 「我住在羅馬」是否有效? – walen