[Resolved] Como copiar arquivo de imagem de um local para outro em Java

Hi people !

Gostaria de saber como faço para copiar um arquivo de imagem de um local Ex: /documents and settings/images/image.png para outro local.
Sei que existe uma maneira com ImageIO e File mas não tenho idéia de como fazer no momento.

Waiting for answers…

[quote=CyberX]Hi people !

Gostaria de saber como faço para copiar um arquivo de imagem de um local Ex: /documents and settings/images/image.png para outro local.
Sei que existe uma maneira com ImageIO e File mas não tenho idéia de como fazer no momento.

Waiting for answers…[/quote]

[quote=CyberX]Hi people !

Gostaria de saber como faço para copiar um arquivo de imagem de um local Ex: /documents and settings/images/image.png para outro local.
Sei que existe uma maneira com ImageIO e File mas não tenho idéia de como fazer no momento.

Waiting for answers…[/quote]

Use o FileChannel.

Exemplo: http://www.java2s.com/Tutorial/Java/0180__File/CopyingFilesusingFileChannel.htm

Consegui fazer dessa forma.

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Class create a copy of image.
 * @author CyberX
 */

public class CopyImages {
	public static void copy(String inFile, String outFile) throws IOException {
		copy(new File(inFile), new File(outFile));
	}

	public static void copy(File inFile, File outFile) throws IOException {
		// Verify if exists the file.
		if (!inFile.exists()) {
			System.out.println("File Not Found!");
			return;
		}
		// Buffering the image
		BufferedImage image = ImageIO.read(inFile);
		BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outFile));
		ImageIO.write(image, "png", os);

		System.out.println("File created in: " + outFile.getAbsoluteFile());
		os.close();
	}

	public static void main(String[] args) {
		try {
			copy("Input file", "Output File");
		} catch (IOException e) {
			System.out.println("Not possible to copy !!!");
		}
	}
}

Realmente creio que com o FileChannel seja mais otimizado esse processo. Vou avaliar relação a performance.

Segue com FileChannel:

	public static void main(String[] args) throws IOException {
	    File fromFile = new File("/test.png");
	    File toFile = new File("/copy.png");
	    FileInputStream inFile = new FileInputStream(fromFile);
	    FileOutputStream outFile = new FileOutputStream(toFile);
	    FileChannel inChannel = inFile.getChannel();
	    FileChannel outChannel = outFile.getChannel();
	    int bytesWritten = 0;
	    long byteCount = inChannel.size();
	    while (bytesWritten < byteCount) {
	      bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
	    }
	    inFile.close();
	    outFile.close();
	  }

Font: Java2s

Só não esqueça que em um código melhor, o primeiro passo é adicionar um try - catch com o bloco finally :slight_smile:
De qualquer forma, faça os testes e tire “a prova dos nove” .

Abraços.

Valeu pela dica !

Seria mais ou mesmo assim ?

	static void copy(File from, File to) throws IOException{
		FileInputStream in = new FileInputStream(from);
		FileOutputStream out = new FileOutputStream(to);
		FileChannel inChannel = in.getChannel();
		FileChannel outChannel = out.getChannel();
		try{
		    int bytesWritten = 0;
		    long byteCount = inChannel.size();
		    System.out.println("Creating File...");
		    // TODO Understand this process
		    while(bytesWritten < byteCount){
		    	bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
		    }
		    System.out.println("File created in: " + to.getAbsolutePath());
		}catch(IOException e){
			System.err.println("Error to copy file from: " + from.getAbsolutePath() +" to " + to.getAbsolutePath());
		}finally{
			// Closing Streams and Channels
			in.close();
			out.close();
			inChannel.close();
			outChannel.close();
		}
	}

[quote=CyberX]Seria mais ou mesmo assim ?

static void copy(File from, File to) throws IOException{ FileInputStream in = new FileInputStream(from); FileOutputStream out = new FileOutputStream(to); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); try{ int bytesWritten = 0; long byteCount = inChannel.size(); System.out.println("Creating File..."); // TODO Understand this process while(bytesWritten < byteCount){ bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel); } System.out.println("File created in: " + to.getAbsolutePath()); }catch(IOException e){ System.err.println("Error to copy file from: " + from.getAbsolutePath() +" to " + to.getAbsolutePath()); }finally{ // Closing Streams and Channels in.close(); out.close(); inChannel.close(); outChannel.close(); } } [/quote]

Sim, mas eu faria isso:

static void copy(File from, File to) throws IOException{ FileInputStream in = null; FileOutputStream out = null; FileChannel inChannel = null; FileChannel outChanne = null; try{ in = new FileInputStream(from); out = new FileOutputStream(to); inChannel = in.getChannel(); outChannel = out.getChannel(); int bytesWritten = 0; long byteCount = inChannel.size(); System.out.println("Creating File..."); // TODO Understand this process while(bytesWritten < byteCount){ bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel); } System.out.println("File created in: " + to.getAbsolutePath()); }catch(IOException e){ System.err.println("Error to copy file from: " + from.getAbsolutePath() +" to " + to.getAbsolutePath()); }finally{ // Closing Streams and Channels if(null != in) in.close(); if(null != out) out.close(); if(null != inChannel) inChannel.close(); if(null != outChannel) outChannel.close(); } }

Depois é pensar se pode aumentar/melhorar a perfomance.

Ok, entendi vou avaliar, previamente agradeço a ajuda.

Tks.