文件的切割:創(chuàng)建一個(gè)輸入流來讀取文件,創(chuàng)建多個(gè)輸出流來寫碎片文件扔枫,寫完一個(gè)輸出流就關(guān)一個(gè)汛聚。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoDemo {
public static void main(String[] args) throws IOException {
File f = new File("e:/lishuai.txt");
cutmethord(f);
}
private static void cutmethord(File f) throws IOException {
// TODO Auto-generated method stub
FileInputStream fi = new FileInputStream(f);
int len = 0;
// 定義碎片文件名
int count = 1;
byte[] b = new byte[1024];
while ((len = fi.read(b)) != -1) {
FileOutputStream fo = new FileOutputStream("e:/"+count+".txt");
fo.write(b, 0, len);
fo.close();
count++;
}
fi.close();
}
}
文件的關(guān)閉:文件的合并與文件的切割思想相反,先創(chuàng)建一個(gè)輸出流對(duì)象短荐,之后遍歷碎片文件倚舀,邊讀邊寫。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoDemo {
public static void main(String[] args) throws IOException {
methord();
}
private static void methord() throws IOException {
// TODO Auto-generated method stub
FileOutputStream fo = new FileOutputStream("e:/lishuai.txt");
for (int i = 1; i <= 3; i++) {
FileInputStream fi = new FileInputStream("e:/" + i + ".txt");
int len = 0;
byte[] b = new byte[1024];
while ((len = fi.read(b)) != -1) {
fo.write(b, 0, len);
}
fi.close();
}
fo.close();
}
}