java中IO文件拷贝的基本方法
package IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOCopy {
public static void main(String args[]) {
File f = new File(“D:/java_foundation/src/IO/2.txt”);
File f1 = new File(“D:/java_foundation/src/IO/mmm”);
InputStream i = null;
OutputStream o = null;
try {
i = new FileInputStream(f);
o = new FileOutputStream(f1);
byte[] b = new byte[1024];
int len = -1;
while((len = i.read(b))!=-1) {
String s = new String(b,0,len);
}
o.write(b);
o.flush();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
o.close();
i.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}