Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement