java
package myio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoDemo4 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\你的文件路径\\b.txt");
FileOutputStream fos = new FileOutputStream("D:\\你的文件路径\\copy.txt");
int b;
while (( b = fis.read()) != -1){
fos.write(b);
}
fis.close();
fos.close();
}
}
弊端和解决方法
FileInputStream一次只能读取一个字节,速度太慢
如果文件过大可以使用多个字节读取
java
package myio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoDemo4 {
public static void main(String[] args) throws IOException {
long sta = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("D:\\你的文件路径\\ceshi.mp3");
FileOutputStream fos = new FileOutputStream("D:\\你的文件路径\\copy.mp3");
int len;
byte[] bytes = new byte[1024 * 1024 * 5];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println(end - sta);
}
}