Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. // copia os dados
  4. InputStream in;
  5. // escreve os dados
  6. OutputStream out;
  7. try{
  8. // arquivos que vamos copiar
  9. File toFile = new File("toFile.txt");
  10. // destino para onde vamos mover o arquivo
  11. File fromFile = new File("newfolder/newFile.txt");
  12. //verifica se o arquivo existe
  13. if(!fromFile.exists()){
  14. //verifica se a pasta existe
  15. if(!fromFile.getParentFile().exists()){
  16. //cria a pasta
  17. fromFile.getParentFile().mkdir();
  18. }
  19. // cria o arquivo
  20. fromFile.createNewFile();
  21. }
  22. in = new FileInputStream(toFile);
  23. out = new FileOutputStream(fromFile);
  24. // buffer para transportar os dados
  25. byte[] buffer = new byte[1024];
  26. int length;
  27. // enquanto tiver dados para ler..
  28. while((length = in.read(buffer)) > 0 ){
  29. // escreve no novo arquivo
  30. out.write(buffer, 0 , length);
  31. }
  32.  
  33. in.close();
  34. out.close();
  35. //apaga o arquivo antigo
  36. toFile.delete();
  37.  
  38. }catch(IOException e){
  39. e.printStackTrace();
  40. }
  41.  
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement