[Resolved] Como copiar arquivo de imagem de um local para outro em Java
9 respostas
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.
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…
nel
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.
importjava.awt.image.BufferedImage;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjavax.imageio.ImageIO;/** * Class create a copy of image. * @author CyberX */publicclassCopyImages{publicstaticvoidcopy(StringinFile,StringoutFile)throwsIOException{copy(newFile(inFile),newFile(outFile));}publicstaticvoidcopy(FileinFile,FileoutFile)throwsIOException{// Verify if exists the file.if(!inFile.exists()){System.out.println("File Not Found!");return;}// Buffering the imageBufferedImageimage=ImageIO.read(inFile);BufferedOutputStreamos=newBufferedOutputStream(newFileOutputStream(outFile));ImageIO.write(image,"png",os);System.out.println("File created in: "+outFile.getAbsoluteFile());os.close();}publicstaticvoidmain(String[]args){try{copy("Input file","Output File");}catch(IOExceptione){System.out.println("Not possible to copy !!!");}}}
CyberX
Realmente creio que com o FileChannel seja mais otimizado esse processo. Vou avaliar relação a performance.
Só não esqueça que em um código melhor, o primeiro passo é adicionar um try - catch com o bloco finally
De qualquer forma, faça os testes e tire “a prova dos nove” .
Abraços.
CyberX
Valeu pela dica !
CyberX
Seria mais ou mesmo assim ?
staticvoidcopy(Filefrom,Fileto)throwsIOException{FileInputStreamin=newFileInputStream(from);FileOutputStreamout=newFileOutputStream(to);FileChannelinChannel=in.getChannel();FileChanneloutChannel=out.getChannel();try{intbytesWritten=0;longbyteCount=inChannel.size();System.out.println("Creating File...");// TODO Understand this processwhile(bytesWritten<byteCount){bytesWritten+=inChannel.transferTo(bytesWritten,byteCount-bytesWritten,outChannel);}System.out.println("File created in: "+to.getAbsolutePath());}catch(IOExceptione){System.err.println("Error to copy file from: "+from.getAbsolutePath()+" to "+to.getAbsolutePath());}finally{// Closing Streams and Channelsin.close();out.close();inChannel.close();outChannel.close();}}
nel
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();
}
}
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.
CyberX
Ok, entendi vou avaliar, previamente agradeço a ajuda.