实现将E:\itcast\creat.txt内容复制到Normal\out.txt(idea项目)中
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyDate {
public static void main(String[] args) throws IOException {
FileInputStream in=new FileInputStream("E:\itcast\creat.txt");//创建字节输入流对象
FileOutputStream out=new FileOutputStream("Normal\out.txt");//创建字节输出流对象
int len;
while((len=in.read())!=-1){
out.write(len);//一次读写一个字节
}
in.close();
out.close();//一定记住关闭资源
}
}
复制图片:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyImg {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("E:\itcast\1.jpg");//将E盘下的图片地址给字节输入流对象In
FileOutputStream out = new FileOutputStream("Gzy_BasicJava\newCopy.jpg");//字节输出流地址
byte[] bytes = new byte[1024];
int len ;
while ((len = in.read(bytes)) != -1) {
out.write(bytes,0,len);
}
in.close();//释放资源
out.close();
}
}