2014-02-14 1 views
1

로 나는저장 자바 파일

File file = new File("/home/aa.db"); 

그때 내가

file.delete(); 전화를 가지고 있지만 내가 전에이 파일을 저장할 다른 위치에 다른 이름으로 삭제할. - 비주얼 툴없이 코드에서 어떻게 할 수 있습니까?

init 파일을 제거해야하기 때문에 복사가 필요합니다.

import java.io.File; 

public class Example { 
    public static void main(String[] args) { 

     File file = new File("C://aa.db"); 
     File tmpFile = new File("C://temp", file.getName()); 

     if(file.renameTo(tmpFile)) { 
      if(tmpFile.delete()) { 
       System.out.println(tmpFile.getName() + " was deleted!"); 
      } else { 
       System.out.println("Delete operation failed."); 
      } 
     } 
    } 
} 

출력 : aa.db was deleted!

+2

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy –

+0

왜 이름을 변경하지 않습니까? http://stackoverflow.com/questions/1158777/renaming-a-file-using-java – doctorlove

+0

import nio.file을 추가했습니다. 내가 시험해 보는 것보다. (하지만 복사본이 없다) java.nio.file보다 썼다. (하지만 정적 메서드로 여기에 복사본이 없습니다.) = ( – curiousity

답변

1

새로운 디렉토리에 파일을 이동하는 샘플을하지 않을 것이다 파일의 데이터 (귀하의 필요)를 InputStream에 입력 한 다음 사용하십시오 File

public static long copy(InputStream in,Path target,CopyOption... options) 
       throws IOException 

복사 파일로 입력 스트림에서의 모든 바이트의 기능. 돌아 왔을 때 입력 스트림이 스트림의 끝 부분에있게됩니다.

이렇게하기 전에 먼저 데이터를 복사 할 대상 경로를 만들어야합니다.

0

내용을 읽어

File file = new File("/home/aa.db"); 
File dir = new File("dir"); 

if (file.renameTo(new File(dir, file.getName()))) { 
    // processing here 
} 

관련 예 - 이름 바꾸기는 것은 여기