文件复制
import java.io.*;//导入io包
class test1
{
public static void main(String[] args) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
//为fin实例化,指向文件myfile.txt
fin=new FileInputStream("myfile.txt");
//为fout实例化,指向文件yourfile.txt
fout=new FileOutputStream("yourfile.txt");
do
{
i=fin.read();
if(i!=-1)
fout.write(i);
}while(i!=-1);
fin.close();//关闭输入流
fout.close();
System.out.println("myfile.txt内容已经被复制到yourfile.txt文件中");
}
}
图片复制
import java.io.*;
public class test2
{
public static void main(String[] args) throws IOException
{
FileInputStream fi=new FileInputStream("wyn.jpg");
FileOutputStream fo=new FileOutputStream("美景.jpg");
System.out.println("文件的大小="+fi.available());
byte[] b=new byte[fi.available()];
int i=fi.read(b);
fo.write(b);
System.out.println("文件已拷贝并更名");
fi.close();
fo.close();
}
}