import java.io.*;
import java.nio.channels.FileChannel;
public class IOTest {
public static void main(String[] args) throws IOException {
String filePath = "C:/Users/Administrator/Desktop/";
String fileName = "test.txt";
String fileName2 = "copytest.txt";
File f = new File(filePath+fileName);
File f2 = new File(filePath+fileName2);
BufferedReader bfr = new BufferedReader(new FileReader(f));
String str = null;
while((str = bfr.readLine()) != null){
System.out.println(str);
}
if(f.exists()){
FileOutputStream fos = new FileOutputStream(f);
String s = "123456";
byte[] b = s.getBytes();
fos.write(b);
fos.close();
} else {
System.err.println("文件不存在!");
}
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(f2);
char[] c =new char[1024];
fr.read(c);
fw.write(c);
fw.flush();
fw.close();
int num = 5;
int len =(int) f.length()/num;
System.out.println(f.length());
if((int) f.length()%num!=0){
num++;
}
fr.read(c);
for(int i=0;i<num;i++){
FileWriter fout = new FileWriter(filePath+"cut"+i+".txt");
fout.write(c, len*i, len);
fout.flush();
}
char[] ch = new char[1024];
int length = 0;
for(int i=0;i<num;i++){
File ff =new File(filePath+"cut"+i+".txt");
FileReader fin =new FileReader(ff);
fin.read(ch, length, (int) ff.length());
length += (int)ff.length();
}
FileWriter fww = new FileWriter(filePath + "sum.txt");
fww.write(ch);
fww.flush();
}
}