File Copy
public class Test4_Copy {
public static void main(String[] args) throws Exception {
File from = new File("D:\\teach\\a\\1.txt");
File to = new File("D:\\teach\\a\\to.txt");
copy(from, to);
}
public static void copy(File from, File to) throws Exception {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
int b = 0;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
}
Batch reading and writing
public class Test4_Copy {
public static void main(String[] args) throws Exception {
File from = new File("D:\\teach\\a\\1.txt");
File to = new File("D:\\teach\\a\\to.txt");
copyByte(from, to);
copyArray(from, to);
}
private static void copyArray(File from, File to) throws Exception {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
int b = 0;
byte[] bs = new byte[8*1024];
while ((b = in.read(bs)) != -1) {
out.write(bs);
}
in.close();
out.close();
}
public static void copyByte(File from, File to) throws Exception {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
int b = 0;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
}
}