我對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