我本来想用FileReader类和FileWriter类实现图形文件的复制。但这个程序并不能实现 图形的复制。

我的想法:可能是FileReader和FileWriter只能处理字符流,即纯文本文件;而图形好像是
二进制文件;所以不能实现图形复制。如果是这样,哪有没有二进制的图形文件呢?

package inputandoutput;
import java.io.*;
public class copyphotoFileReaderandFileWriter
{
public static void main(String[] args) throws IOException
{
FileReader fi=new FileReader("e:\\workspace\\photo.jpg");
FileWriter fo=new FileWriter("e:\\workspace\\copyphoto.jpg");

char ch[]=new char[10000];
fi.read(ch);//将图形文件读入ch字符数组
fo.write(ch);//将ch字符数组数据写入新文件copyphoto
System.out.println("图形文件已被复制且更名");
fi.close();
fo.close();
}

}

建议看一下apache FileUtils.copyFile的源码,是可以copy图片的,你说的的图形文件是图片吧?

public class CopyFile {
public static void doCopyFile() throws IOException{
File srcFile = new File("e:\\test.jpg");
File destFile = new File("e:\\demo\\test.jpg");
FileInputStream input = new FileInputStream(srcFile);
try {
FileOutputStream output = new FileOutputStream(destFile);
try {
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) {
// ignore
}
}
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
}
}

}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答