用Java编写一个程序,将一个图像文件复制到指定的文件夹中
这是我们公司基类里的一个方法希望对你有帮助。。/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } }
java怎样复制图片
图片本质上还是文件,所以就像复制文件一样就可以了。下面是一个演示程序:
public class CopyImage{ public static void main(String[] args) throws Exception { FileInputStream fi=new FileInputStream("image.jpg"); BufferedInputStream in=new BufferedInputStream(fi); FileOutputStream fo=new FileOutputStream("cimage.jpg"); BufferedOutputStream out=new BufferedOutputStream(fo); byte[] buf=new byte[4096]; int len=in.read(buf); while(len!=-1) { out.write(buf, 0, len); len=in.read(buf); } out.close(); fo.close(); in.close(); fi.close(); }} 运行程序是改一改图片的路径,另外在实际代码中最后不要想上面的代码直接抛出这样的异常。