重命名的文件已损坏

时间:2018-09-04 19:13:35

标签: file copy rename move

我试图单击链接并在下载内容中找到文件,然后需要重命名文件并将其移动到所需的文件夹。它可以工作,但已移动的重命名文件永远不会有任何MB。当我打开它时,它也说已损坏。我怀疑我的代码由于某种原因未能正确移动文件。

File dir = new File("C:\\Users\\"+zid+"\\Downloads");
File NFile = new File("C:\\Users\\"+zid+"\\Documents\\TestDataFolder"); 

System.out.println("dir is" + dir); 

FilenameFilter filter = new FilenameFilter() {
    public boolean accept (File dir, String name) { 
       System.out.println(name.contains(CRPT));
       return name.contains(CRPT);                 
    } 
}; 

Thread.sleep(2000);
String[] children1 = dir.list(filter);

if (children1 == null) {
    System.out.println("Either dir does not exist or is not a directory"); 
} else { 
    // for (int d=0; d< children.length; d++) {
    int d = 0;
    String filename = children1[d];
    System.out.println(filename);
    File newfile = new File(NFile+"\\"+caseID+ (filename));
    System.out.println(newfile);
    System.out.println(NFile+"\\"+caseID+ (filename));
    //    driver.findElement(By.xpath("//input[@id='mainForm:comprehensive']")).sendKeys("C:\\Users\\Z246379\\Documents\\Test beds\\" + filename);
    //    } 

    // File (or directory) with old name
    File file = new File(filename);

    // File (or directory) with new name
    File file2 = new File(NFile+"\\"+caseID+ (filename));

    if (file2.exists())
        throw new java.io.IOException("file exists");

    // Rename file (or directory)
    boolean success = file.renameTo(file2);

    if (!success) {
        // File was not successfully renamed
    }

    java.io.FileWriter out= new java.io.FileWriter(file2, true /*append=yes*/);
} 

0 个答案:

没有答案