CyberX
Abril 13, 2012, 12:30pm
#1
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…
CyberX
Abril 13, 2012, 12:33pm
#2
[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]
nel
Abril 13, 2012, 12:36pm
#3
[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
CyberX
Abril 13, 2012, 12:46pm
#4
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 !!!");
}
}
}
CyberX
Abril 13, 2012, 12:48pm
#5
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
nel
Abril 13, 2012, 12:55pm
#6
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
Abril 13, 2012, 1:20pm
#8
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();
}
}
nel
Abril 13, 2012, 1:29pm
#9
[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.
CyberX
Abril 13, 2012, 1:41pm
#10
Ok, entendi vou avaliar, previamente agradeço a ajuda.
Tks.