我对Java很陌生,最近我正在制作一个程序,它从一个目录读取图像文件(jpg),并将它们写入(复制)到另一个目录。用Java在本地文件系统之间无imageio读写图像
我无法使用imageio或移动/复制方法,而且我还必须检查由R/W操作导致的耗时。
问题是我在下面写了一些代码,它运行,但目标中的所有输出图像文件都有0字节,根本没有内容。 当我打开结果图像时,我只能看到没有字节的黑色屏幕。
public class image_io {
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
// getting path
File directory = new File("C:\\src");
File[] fList = directory.listFiles();
String fileName, filePath, destPath;
// date for time check
Date d = new Date();
int byt = 0;
long start_t, end_t;
for (File file : fList)
{
// making paths of source and destination
fileName = file.getName();
filePath = "C:\\src\\" + fileName;
destPath = "C:\\dest\\" + fileName;
// read the images and check reading time consuming
try
{
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
do
{
start_t = d.getTime();
}
while ((byt = bis.read()) != -1);
end_t = d.getTime();
System.out.println(end_t - start_t);
} catch (Exception e) {e.printStackTrace();}
// write the images and check writing time consuming
try
{
fos = new FileOutputStream(destPath);
bos = new BufferedOutputStream(fos);
int idx = byt;
start_t = d.getTime();
for (; idx == 0; idx--)
{
bos.write(byt);
}
end_t = d.getTime();
System.out.println(end_t - start_t);
} catch (Exception e) {e.printStackTrace();}
}
}
}
是的FileInput/OutputStream中不支持的图像文件? 或者在我的代码中有一些错误?
请,有人帮助我..
您可能想检查一下,即使您可能不会使用Apache方法(我认为第一种方法最适合您的需要):http://examples.javacodegeeks。COM /核心的Java/IO /文件/ 4路到复制文件中的Java / – T145