如何在文件夹中添加图像

时间:2015-08-27 17:51:58

标签: java

我创建了一个文件夹(C:/test):

File dir = new File("C:/test");     
dir.mkdirs();

我想在此文件夹中添加图片。该图像位于项目文件夹/resources/1.png中。

try {
    FileInputStream fin=new FileInputStream("1.png");
    byte[] buffer = new byte[fin.available()];
    fin.read(buffer, 0, fin.available());
    FileOutputStream fos=new FileOutputStream("C://Dir//1.png");
    fos.write(buffer, 0, buffer.length); 
} catch(IOException ex)
{
    System.out.println(ex.getMessage());
}

1 个答案:

答案 0 :(得分:0)

为什么不使用内置的Java ImageIO

try {
   File input = new File("resources/1.png");  // your input file
   File output = new File("C://test/1.png");  // your output file
   BufferedImage bi = ImageIO.read(in);  
   ImageIO.write(bi, "png", out);  // rendered image, file type, file
}catch (Exception e){
  e.printStackTrace();
}

希望这有帮助。