2017-04-04 5 views
1

모두는 fileutils에 속하는 명령어를 사용하여 b를 점 점 A에서 파일을 이동하는 방법을 간단하게 말을 계속하지만이 파일 :(내가 디렉토리 곳에서/온도/폴더가Java - FileUtils를 사용하여 파일을 이동하는 방법?

이동 문제가 많은 데 .jar 파일이 임시 폴더에 있습니다. 기본적으로 .jar 파일 옆에있는 디렉터리로 옮기고 싶지만 그럴 수 없습니다.

일부 코드가 있지만 나는 그것조차 가까운 알고

public void replaceFile() { 
    String absolutePath = getPath(); 
    Path from = Paths.get(absolutePath + "\\temp\\test.txt"); 
    Path to = Paths.get(absolutePath + "\\test.txt"); 

    try { 
     FileUtils.moveFile(FileUtils.getFile(from.toAbsolutePath().toString()), FileUtils.getFile(to.toAbsolutePath().toString())); 
     JOptionPane.showMessageDialog(null, "test"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public String getPath() { 
    File jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()); 
    //JOptionPane.showMessageDialog(null, jarDir.getAbsolutePath()); 
    return jarDir.getAbsolutePath(); 
} 

어떤 도움에 감사드립니다 :

\
+1

당신은 – freedev

+0

당신이 이동하기 전에 소스 파일의 경로를에 println을 시도했다 – peterxz

+0

이동되지 않는 파일을 발견 한 잘못된 행동에 무엇을 추가해야합니까? – freedev

답변

2

왜 소스 코드를 보면 Moving a File or Directory

Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);

UPDATE

이 자바 API를 사용하지 않는 나는 다음과 같은 구현을 제안 : 난을 관리

Path from = Paths.get(absolutePath, "/temp/test.txt"); 
Path to = Paths.get(absolutePath, "/test.txt"); 

try { 
    Files.move(from, to, StandardCopyOption.REPLACE_EXISTING); 
    JOptionPane.showMessageDialog(null, "test"); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

시도해 보았는데 – peterxz

+0

작동하는 예제를 줄 수 있습니까? 내가 엉망이 어디 있는지보고 – peterxz

+0

나는 내 대답을 업데이 트했습니다. 내 컴퓨터에서 로컬로이 버전을 사용해 보았습니다. 원본 및 대상 경로가 올바르게 작동하면이 버전을 로컬로 시도했습니다. – freedev

1

확인 getPath() 메서드가 재미있는 경로를 반환하고 거기에서 실패 했으므로 코드가 작동합니다.

public void downloadJar() { 
    String absolutePath = getPath(); 
    String from = absolutePath + "\\temp\\test.txt"; 
    String to = absolutePath + "\\test.txt"; 
    File fileTo = new File(to); 
    File fileFrom = new File(from); 


    try { 
     FileUtils.moveFile(fileFrom, fileTo); 
     JOptionPane.showMessageDialog(null, "test"); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     JOptionPane.showMessageDialog(null, "io exce"); 
    } 

} 

public String getPath() { 
    return System.getProperty("user.dir"); 
} 

모두 감사합니다

관련 문제