0
How do I manage to change the directory of a java file? I’ve done a lot of research and I couldn’t find a functional way to move files to another java directory.
0
How do I manage to change the directory of a java file? I’ve done a lot of research and I couldn’t find a functional way to move files to another java directory.
2
Try it this way:
public static void main(String[] args) {
// copia os dados
InputStream in;
// escreve os dados
OutputStream out;
try{
// arquivos que vamos copiar
File toFile = new File("toFile.txt");
// destino para onde vamos mover o arquivo
File fromFile = new File("newfolder/newFile.txt");
//verifica se o arquivo existe
if(!fromFile.exists()){
//verifica se a pasta existe
if(!fromFile.getParentFile().exists()){
//cria a pasta
fromFile.getParentFile().mkdir();
}
// cria o arquivo
fromFile.createNewFile();
}
in = new FileInputStream(toFile);
out = new FileOutputStream(fromFile);
// buffer para transportar os dados
byte[] buffer = new byte[1024];
int length;
// enquanto tiver dados para ler..
while((length = in.read(buffer)) > 0 ){
// escreve no novo arquivo
out.write(buffer, 0 , length);
}
in.close();
out.close();
//apaga o arquivo antigo
toFile.delete();
}catch(IOException e){
e.printStackTrace();
}
}
It worked, thank you.
vlw was the only one that worked after much research
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
http://www.arquivodecodigos.net/dicas/java-como-mover-um-arquivo-para-outro-diretorio-457.html
– Marcelo
It didn’t work, as I said I had already researched enough and had already seen this example on this site.
– Higor